簡體   English   中英

LINQ - 獲取列表中列表中的所有項目?

[英]LINQ - Get all items in a List within a List?

我正在努力學習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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM