LINQ – 获取列表中列表中的所有项目?
我正在努力学习LINQ的学习曲线,我真的可以使用一些帮助。 我不知道我想要的是否可能,但如果我不得不下注,我敢打赌。
我目前有一个名为_tables的对象列表,其中每个对象都有一个通过属性“索引”公开的另一个对象列表。 基本上,我想最终得到一个List,其中包含来自所有_tables的所有索引。
这是我到目前为止所拥有的:
var indexes = from TableInfo tab in _tables where tab.Indexes.Count > 0 select tab.Indexes;
不幸的是,这似乎给了我另一个列表列表,但只有索引列表包含多个值…有没有办法将所有这些列表放在一起没有循环?
您想使用SelectMany
扩展方法。
_tables.SelectMany(t => t.Indexes)
除了tbischel的答案之外,您要查找的查询表达式版本如下。
var indexes = from TableInfo tab in _tables from index in tab.Indexes select index;
您不需要where子句,也不需要告诉它是什么选项卡
你需要使用SelectMany
var indexes = (from tab in _tables).SelectMany(t => t.Indexes)
或者你可以这样做
var indexes = from tab in _tables from t in tab.Indexes select t;
这应该是一个更熟悉的syntaz
var rows = from item in table select item;
- Linq to sql将IQueryable转换为Dataset
- 在数字后面有NumericUpDown控件内的文本
- System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()中的FileNotFoundException
- 使用属性读取XML的最简单方法
- 通过特定的Source读取Windows事件日志
- System.Web可以与带有完整框架的ASP.Net Core一起使用
- 线程已退出代码0(0x0),没有未处理的exception
- 如何用反斜杠替换正斜杠
- Asp.Net Identity Localization PublicKeyToken