简体   繁体   中英

Linq Query to Lambda Expression

I've been trying to use a lambda for this :

var y = from r in rs.Returns from z in r.Tags where z.Name.Contains(c) select r;

I tried var r = rs.Returns.Where(x=>x.Tags.Where(x=>x.Name.Contains(c))); but it didnt work. What is the correct lambda so I dont have to use y & z

You need a SelectMany to translate the second "from" clause:

var y = rs.Returns
          .SelectMany(r => r.Tags, (r, z) => new { r, z })
          .Where(pair => pair.z.Name.Contains(c))
          .Select(pair => pair.r);

That's a pretty direct translation. Another alternative would be to use:

var y = rs.Returns.Where(r => r.Tags.Any(z => z.Name.Contains(c)));

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