如何将此语句从 (LINQ To Object) Criteria 转换为 Lambda 表达式?
我对 Linq
还很陌生。只是想知道如何将这个条件表达为 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
};
我已经尝试过:
var newlist = personList.Select(p => new { id = p.Id, toys = p.Toys });
但我不知道将 where 子句放在哪里。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这大致相当于:
如果您有 LinqPad,您可以单击
λ
选项卡并查看语句的等效 lambda 语法。This is roughly equivalent:
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) =>;新的{p1.Id,t})
something like
personList.Where(p => p.Animal.Any(a => a.Name == "Cat")).SelectMany(p => p.Toys, (p1,t) => new { p1.Id, t})