简体   繁体   中英

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?

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:

List<Int32> list1 = GetList();
List<Int32> list2 = new List<Int32>(list1);

With 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM