我有这个查询:
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
项目,任何想法为什么?
答案 0 :(得分:5)
您需要SelectMany
而不是Select
。您的查询基本上等同于:
return this.Requests
.Where(r => r.Status == "Authorised")
.SelectMany(r => r.Items)
.Sum(i => i.Value);
请注意,原始查询在多行上也会更清晰......