我有一個包含多個嵌套集合的模型。例如...在LINQ中列出深層嵌套的子實體
My Sales Record
Contains a collection of Customers
Each Customer contains a collection of Orders
Each Order contains a collection of Items
我希望能夠創建一個與銷售記錄相關的所有項目的列表,而不會導致編寫嵌套的foreach循環。我試過...
var items = SalesRecord.SelectMany(r => r.Customers)
.SelectMany(c => c.Orders)
.Select(o => o.Items);
但這不起作用。
這在LINQ中可以實現嗎?需要
'SelectMany'不會顯示結果直到你實際循環結果集。你在做這個嗎? –