[英]How to convert this linq query to lambda?
我有這個查詢:
return (from r in this.Requests where r.Status == "Authorised" from i in r.Items select i).Sum(i => i.Value);
我嘗試將它轉換為lambda,因為我現在更喜歡它,所以我做了:
var sum = Requests.Where(x=>x.Status == "Authorised").Select(x=>x.Items).Sum(x=>x.Value);
- >在這里我沒有Value
物品,任何想法為什么?
您需要SelectMany
而不是Select
。 您的查詢基本上相當於:
return this.Requests
.Where(r => r.Status == "Authorised")
.SelectMany(r => r.Items)
.Sum(i => i.Value);
請注意,您的原始查詢在多行上也會更清晰......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.