Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中使用linq将值列表分配给另一个列表_C#_Linq - Fatal编程技术网

C# 在c中使用linq将值列表分配给另一个列表

C# 在c中使用linq将值列表分配给另一个列表,c#,linq,C#,Linq,我希望基本上使用Linq实现相同的代码逻辑,否则重新分解代码将非常好 List<PublisherDto> listOfMappedPublishers = new List<PublisherDto>(); listOfPublishers.ForEach(pub => { PublisherDto publisher = new PublisherDto(); publisher.ApiKey = pub.ApiKey;

我希望基本上使用Linq实现相同的代码逻辑,否则重新分解代码将非常好

 List<PublisherDto> listOfMappedPublishers = new List<PublisherDto>();

 listOfPublishers.ForEach(pub =>
 {
     PublisherDto publisher = new PublisherDto();

     publisher.ApiKey = pub.ApiKey;
     publisher.Name = pub.Name;
     publisher.Password = pub.Password;

     listOfMappedPublishers.Add(publisher);
 });

 return listOfMappedPublishers;
您可以按如下方式使用Select和ToList:

List<PublisherDto> listOfMappedPublishers = listOfPublishers.Select(i=> new PublisherDto()
{ 
    ApiKey = i.ApiKey,
    Name = i.Name,
    Password  = i.Password
}).ToList();

是什么阻止你这么做的?事实上,这个问题看起来像是我的代码,不符合主题。你确定你真的在使用C 4.0版的.NET Core吗?非常感谢,现在看起来好多了:不客气,请单击复选标记将答案标记为已接受,这样可以帮助其他人。确定原因: