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.
something like
personList.Where(p => p.Animal.Any(a => a.Name == "Cat")).SelectMany(p => p.Toys, (p1,t) => new { p1.Id, t})
精彩评论