使用lambda将两个表连接到模型

时间:2017-10-25 14:30:09

标签: c# join lambda

我在代码中加入了2个表。代码似乎有效。但是当我尝试Debug.WriteLine()时,它是空的。

我见过解决方案。但不是模特。

IQueryable<Order_OrderCart_OrderLogisticsModel> model = repository.Orders
    .Join(
    repository.OrderLogisticss,
    o => o.ID,
    ol => ol.OrderID,
    (o, ol) => new { Order = o, OrderLogistics = ol })
    .Where(x => x.OrderLogistics.DepartureDate == 40826)
    .Join(
    repository.OrderCarts,
    o => o.Order.ID,
    oc => oc.OrderID,
    (o, oc) => new Order_OrderCart_OrderLogisticsModel { Order = o.Order, Logistics = o.OrderLogistics, Cart = oc })
    .Where(x => x.Cart.Sealed != false);

foreach (var item in model)
{
    Debug.WriteLine(item.Cart.Sealed);
    Debug.WriteLine(item.Order.ID);
    Debug.WriteLine(item.Logistics.DepartureDate);

}

问题: 所以我的问题是我做错了什么?我有其他代码,只有一个连接,这确实有效。只有这不起作用。

请仅使用lambda代码回答。是的我知道原来的linq有点快,但我喜欢学习lambda。

更新:

生成的SQL似乎工作正常。

SELECT 
    [Extent1].[ID] AS [ID], 
    [Extent1].[StatusID] AS [StatusID], 
    [Extent1].[KlantID] AS [KlantID], 
    [Extent1].[SoortID] AS [SoortID], 
    [Extent1].[isVerwijderd] AS [isVerwijderd], 
    [Extent1].[Aantalkarren] AS [Aantalkarren], 
    [Extent2].[ID] AS [ID1], 
    [Extent2].[OpdrachtID] AS [OpdrachtID], 
    [Extent2].[Vertrekdatum] AS [Vertrekdatum], 
    [Extent2].[Vertrektijd] AS [Vertrektijd], 
    [Extent3].[ID] AS [ID2], 
    [Extent3].[OpdrachtID] AS [OpdrachtID1], 
    [Extent3].[KarID] AS [KarID], 
    [Extent3].[Gesealed] AS [Gesealed], 
    [Extent3].[Aantal] AS [Aantal], 
    [Extent3].[SamenvoegOpdrachtkarID] AS [SamenvoegOpdrachtkarID], 
    [Extent3].[AanmaakDatum] AS [AanmaakDatum]
    FROM   [dbo].[Opdracht] AS [Extent1]
    INNER JOIN [dbo].[OpdrachtLogistiek] AS [Extent2] ON [Extent1].[ID] = [Extent2].[OpdrachtID]
    INNER JOIN [dbo].[OpdrachtKar] AS [Extent3] ON [Extent1].[ID] = [Extent3].[OpdrachtID]
    WHERE (40826 = [Extent2].[Vertrekdatum]) AND ( NOT ((0 = [Extent3].[Gesealed]) AND ([Extent3].[Gesealed] IS NOT NULL)))

更新2:

代码没问题。我想要显示的数据不存在。我犯了一个众所周知的打字错误。我填写了“40826”而不是“40822”。感谢您的帮助。

更新3: 对于想知道我的模型如何的人:

public class Order_OrderCart_OrderLogisticsModel
{
    private Order order;

    public Order Order
    {
        get { return order; }
        set { order = value; }
    }

    private OrderLogistics logistics;

    public OrderLogistics Logistics
    {
        get { return logistics; }
        set { logistics = value; }
    }

    private OrderCart cart;

    public OrderCart Cart
    {
        get { return cart; }
        set { cart = value; }
    }
}

1 个答案:

答案 0 :(得分:0)

为了使您的代码可测试,我宁愿将条件分开加入。最后,将在适当的时候评估条件,但在调试期间,您将能够查看是否存在子查询结果。

var suitableOrderLogistics = repository.OrderLogisticss.Where(ol => 
    ol.DepartureDate == 40826);
var suitableOrderCarts = repository.OrderCarts.Where(oc => oc.Sealed != false);

var model = repository.Orders
.Join(
  suitableOrderLogistics,
  o => o.ID,
  ol => ol.OrderID,
  (o, ol) => new { OrderId = o.Id, Order = o, OrderLogistics = ol })
.Join(
  suitableOrderCarts ,
  o => o.OrderId,
  oc => oc.OrderID,
  (o, oc) => new Order_OrderCart_OrderLogisticsModel { Order = o.Order, Logistics = o.OrderLogistics, Cart = oc });