简体   繁体   中英

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 :

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?

You need SelectMany instead of 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM