C# 在c中使用linq将值列表分配给另一个列表
我希望基本上使用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;
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吗?非常感谢,现在看起来好多了:不客气,请单击复选标记将答案标记为已接受,这样可以帮助其他人。确定原因: