公共课项{...}
public class Order { public List- Items ... } public class Customer { public List
Orders ... }
现在,使用LINQ我需要获得客户购买的所有商品.我怎么能够?
我尝试了类似var items = from o in cust.Orders select o.Items;
但结果是IEnuberable
,我只想一个>
IEnumerable
.
我在这里要求尽量避免编码2个循环.
您需要SelectMany
,它在查询表达式中表示为第二个(和后续)from
子句:
var items = from order in customer.Orders from item in order.Items select item;
或者,忽略查询表达式:
var items = customer.Orders.SelectMany(order => order.Items);