考虑这两个类。
class OrderDetail {int Specifier;}
class Order
{
OrderDetail[] Details;
}
现在我有一个我想要枚举的List,只选择Specifier为1的对象。因为这在SQL中很容易,我认为LINQ带有连接会对此有好处,但我不知道如何构建查询
from o in orders join o in o.Details on o.Id equals od.Id where od.Specifier = 1 select od
这给出了一个错误,即在连接后当前上下文中不存在“o”。 我在这做错了什么?
答案 0 :(得分:1)
orders.SelectMany(o => o.Details.Where(od => od.Specifier == 1))
在您的查询中,您使用相同的序列变量名称o
来获取详细信息,您应该使用join od in o.Details
。但是这里不需要加入,因为订单已包含详细信息:
from o in orderes
from od in o.Details
where od.Specifier == 1
select od