我很难将此sql查询转换为linq查询。我试图通过附加到该订单的订单详细信息选择我的所有订单。在sql中查询是正确的。当我在linq中写它时,一切都变得混乱。这是我的代码:
var result = from order in orders
join orderDetail in orderDetails
on order.OrderID equals orderDetail.OrderID
select new OrderVm
{
OrderId = order.OrderID,
OrderDetails = order.Order_Details.Select(x =>
new OrderDetailVm
{
OrderId = orderDetail.OrderID,
UnitPrice = orderDetail.UnitPrice,
Quantity = orderDetail.Quantity,
ProductId = orderDetail.ProductID
})
};
return result.ToList();
但是在sql中如果我写
select
o.OrderID,
o.CustomerID,
od.UnitPrice,
od.Quantity,
od.productid,
p.ProductName
from Orders o
inner join [Order Details] od
on o.OrderID = od.OrderID
inner join Products p
on p.ProductID = od.ProductID
where o.OrderID = 10248
答案 0 :(得分:0)
从您在嵌套对象中使用Order_Details的方式来看,连接已经存在并且您正在使用的连接是多余的。
var result = from order in orders
select new OrderVm
{
OrderId = order.OrderID,
OrderDetails = order.Order_Details.Select(x =>
new OrderDetailVm
{
OrderId = x.OrderID,
UnitPrice = x.UnitPrice,
Quantity = x.Quantity,
ProductId = x.ProductID
})
};