[英]How do I select all elements in List<Int32> into another List<Int32> with LINQ?
How can I select all elements from list1 into list2 below by using LINQ? 如何使用LINQ从list1到下面的list2中选择所有元素?
List<Int32> list1 = GetList();
List<Int32> list2 = from e in list2 select ?????
Why no one suggest this solution: 为什么没人建议这种解决方案:
List<int> list1 = GetTheList();
List<int> list2 = list1.ToList();
and why don't you feel strange about: 以及为什么您对以下内容感到陌生:
.Select(x => x)
this expression does nothing. 此表达式不执行任何操作。
No need to hit up LINQ just to copy all elements from a list: 无需复制LINQ即可复制列表中的所有元素:
List<Int32> list1 = GetList();
List<Int32> list2 = new List<Int32>(list1);
With LINQ: 使用LINQ:
List<int> list1 = GetList();
List<int> list2 = list1.Select(x => x).ToList();
The easier way would be 更简单的方法是
List<int> list2 = new List<int>(list1);
As below : 如下 :
List<Int32> list1 = GetList();
List<Int32> list2 = (from e in list2 select e).ToList();
or 要么
list1 = GetList();
List<Int32> list2 = new List<Int32>(list1);
I am not sure if this is what you want. 我不确定这是否是您想要的。 But something like this:
但是这样的事情:
List<Int32> list2 = (from e in list2 select e).ToList();
or 要么
List<Int32> list2 =list1.Select (l =>l).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.