[英]How to convert this linq query to lambda?
I have this Query : 我有这个查询:
return (from r in this.Requests where r.Status == "Authorised" from i in r.Items select i).Sum(i => i.Value);
I tried converting it to lambda as I prefer that now, so I did : 我尝试将它转换为lambda,因为我现在更喜欢它,所以我做了:
var sum = Requests.Where(x=>x.Status == "Authorised").Select(x=>x.Items).Sum(x=>x.Value);
--> and here I got no Value
Item, any ideas why? - >在这里我没有
Value
物品,任何想法为什么?
You need SelectMany
instead of Select
. 您需要
SelectMany
而不是Select
。 Your query is basically equivalent to: 您的查询基本上相当于:
return this.Requests
.Where(r => r.Status == "Authorised")
.SelectMany(r => r.Items)
.Sum(i => i.Value);
Note that your original query would have been clearer on multiple lines too... 请注意,您的原始查询在多行上也会更清晰......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.