简体   繁体   English

使用LINQ将列表对象从单个列表添加到新列表

[英]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. myList可以根据大小进行排列,innerList中最多可以包含50个对象。 I get the innerList as a response from an API where the page size is set to 50. 我从页面大小设置为50的API获得innerList作为响应。

i want my new list to have all these 0 - 120 objects in a single list like 我希望我的新列表在单个列表中具有所有这些0-120个对象

newList[0 to 120]

i'm trying using LINQ like this 我正在尝试像这样使用LINQ

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 ? 有人可以帮我使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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