简体   繁体   中英

How to convert this statement from (LINQ To Object) Criteria To Lambda Expression?

I am quite new to Linq . Just wondering how can I express this criteria to Lambda expression?

        var query = from person in personList
                    from toy in person.Toys
                    from animal in person.Animal
                    where animal.Name == "Cat"
                    select new
                    {
                        person.Id,
                        toy
                    };

I have tried this :

var newlist = personList.Select(p => new { id = p.Id, toys = p.Toys });

But I have no idea where to put the where clause. Thanks

This is roughly equivalent:

query = personList.SelectMany(p => p.Animal.Where(a => a.Name == "Cat")
                  .SelectMany(a => p.Toys.Select(t => new
                  {
                      p.Id,
                      toy = t
                  })));

If you've got LinqPad you can click on the λ tab and see the equivalent lambda syntax for your statements.

类似于personList.Where(p => p.Animal.Any(a => a.Name == "Cat")).SelectMany(p => p.Toys, (p1,t) => new { p1.Id, t})

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