gpt4 book ai didi

c# - 如何将连接两个对象的 LINQ 查询语法转换为方法语法?

转载 作者:行者123 更新时间:2023-12-03 20:16:34 24 4
gpt4 key购买 nike

如何以方法语法形式执行此操作:

foreach (var o in from o in orders
from oi in o.OrderItems
where oi.ProductName == "Widgets"
select o.OrderID)
Console.WriteLine("{0}", o);

尝试过但不起作用:

foreach (var o in orders
.SelectMany(o => o.OrderItems, (o, oi) => new {<<idk what to put here>> })
.Where(oi => oi.ProductName == "Widget"))
{
Console.WriteLine("WIDGET: {0}", o.OrderID);
}

最后我使用了这段代码,它成功了!

foreach (var o in orders
.SelectMany(o => o.OrderItems, (o, oi) => new { o.OrderID, oi.ProductName })
.Where(oi => oi.ProductName == "Widgets"))
{
Console.WriteLine("{0}", o.OrderID);
}

最佳答案

方法语法如下:

orders.Where(o=>o.OrderItems.Any(oi=>oi.ProductName == "Widgets")).Select(o=>o.OrderID)

关于c# - 如何将连接两个对象的 LINQ 查询语法转换为方法语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com