简体   繁体   中英

Add List objects from a single list to a new List using LINQ

i've a list collection which looks like

myList[0].innerList[0 to 50]
myList[1].innerList[51  to 100]
myList[2].innerList[101 to 120]

the myList can go as per the size, at max there are 50 objects in the innerList. I get the innerList as a response from an API where the page size is set to 50.

i want my new list to have all these 0 - 120 objects in a single list like

newList[0 to 120]

i'm trying using LINQ like this

var newList = from reg in myList
           select reg.innerList.ToList();

But i get

newList[0].[0 to 50]
newList[1].[51 to 100]

Can anyone help me with the LINQ ?

认为SelectMany应该可以解决问题。

var newList = myList.SelectMany(x => x.innerList);

You are nearly there

var newList = (from reg in myList      // for each inner list
               from range in reg       // for each item in each inner list
               select range).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