LINQ - 获取列表中的一个列表中的所有项目?列表中、项目、LINQ

2023-09-04 08:09:28 作者:品质生活尽在步步高@

我目前正在通过学​​习曲线是LINQ我的路,我真的可以使用一些帮助。我不知道我想要什么是可能的,但如果要我下注,我敢打赌,它是。

I'm currently working my way through the learning curve that is LINQ and I could really use some assistance. I don't know if what I want is possible, but if I had to wager, I bet it is.

我现在有称为_tables对象的列表和每个对象具有在其内通过属性索引暴露的对象的另一列表。从本质上讲,我想结束了一个列表,其中包含来自所有_tables的所有指标。

I currently have a list of objects called _tables and each of these objects has within it another list of objects exposed through the property, "Indexes". Essentially, I'd like to end up with one List that contains all the Indexes from all of the _tables.

这是我到目前为止有:

var indexes = from TableInfo tab
              in _tables
              where tab.Indexes.Count > 0
              select tab.Indexes;

不幸的是,这似乎是给我解释的另一个列表,但只有当索引列表中包含多个值...有没有办法让所有的这些名单在一起,没有循环?

Unfortunately, this seems to be giving me another List of Lists, but only where the Indexes List contains more than one value... Is there some way to get all of these lists together without loops?

推荐答案

您想要使用的SelectMany 扩展方法。

_tables.SelectMany(t => t.Indexes)
 
精彩推荐
图片推荐