简体   繁体   English

如何将此语句从 (LINQ To Object) Criteria 转换为 Lambda 表达式?

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

I am quite new to Linq .我对LinqLinq Just wondering how can I express this criteria to Lambda expression?只是想知道如何将这个标准表达为 Lambda 表达式?

        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.但我不知道把 where 子句放在哪里。 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.如果您有LinqPad,您可以单击λ选项卡并查看语句的等效 lambda 语法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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