LINQ - 获取列表中的一个列表中的所有项目? [英] LINQ - Get all items in a List within a List?

查看:126
本文介绍了LINQ - 获取列表中的一个列表中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过学​​习曲线是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)

这篇关于LINQ - 获取列表中的一个列表中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆