如何在两个字段中使用左连接或条件?

时间:2013-12-23 07:04:37

标签: c# linq c#-4.0 linq-to-entities

invoiceData = from x in context.PdtDeliveryTables 
    join pt in context.DiagramNoTables on x.DiagramID equals pt.DiagramID
    join mt in context.MaterialTables on pt.MaterialID equals mt.MaterialID into t from ct in t.DefaultIfEmpty()
    join odr in context.OrderTables on x.OrderID equals odr.OrderID into sr
    from d in sr.DefaultIfEmpty()
    where (pt.CustomerID == customerID) && (x.DeliveryDate >= (startDate ?? DateTime.MinValue) && x.DeliveryDate <= (endDate ?? DateTime.MaxValue))
    orderby x.DeliveryDate  ascending
    select new InvoiceTable
        {    
        };   

我需要在OR条件下替换这个左连接条件:

join odr in context.OrderTables on x.OrderID equals odr.OrderID into sr
from d in sr.DefaultIfEmpty()

喜欢:

join odr in context.OrderTables on x.OrderID equals 
    x => x.OrderID !=0 
        ? x.OrderNo
        : x.DeliveryNo 
into sr from d in sr.DefaultIfEmpty()
  • 下面是更改的查询,但我仍然获得超时异常
  • 超时已过期。操作完成之前经过的超时时间或服务器没有响应。

                invoiceData = from x in context.PdtDeliveryTables
                             join pt in context.DiagramNoTables on x.DiagramID equals pt.DiagramID
                             join mt in context.MaterialTables on pt.MaterialID equals mt.MaterialID into t from ct in t.DefaultIfEmpty()
                             from d in context.OrderTables.DefaultIfEmpty()
                             where (x.OrderID != 0 && x.OrderID == d.OrderID) || (x.OrderID == 0 && x.DeliveryNo == d.OrderNo)
                             where (pt.CustomerID == customerID) && (x.DeliveryDate >= (startDate ?? DateTime.MinValue) && x.DeliveryDate <= (endDate ?? DateTime.MaxValue))
                             orderby x.DeliveryDate  ascending
               select new InvoiceTable
               {
                    OrderID = x.OrderID,
                    OrderNo = x.OrderID != 0 ? d.OrderNo : x.DeliveryNo,
                    DiagramNo = pt.DiagramNo,
                    ProductName = pt.ProductName,
                    MaterialName = ct.MaterialName,
                    Quantity = x.DeliveryQty,
                    OrderDate = x.DeliveryDate,
                    InvoiceDate = d.InvoiceDate,
                    UnitPrice = d.UnitPrice != null ? d.UnitPrice : pt.SellingPrice,
                    Amount = d.UnitPrice != null ? d.UnitPrice * x.DeliveryQty : pt.SellingPrice * x.DeliveryQty,
                    Printable = true
                };                       
            }
    

1 个答案:

答案 0 :(得分:1)

我删除了x(PdtDeliveryTables)和odr(OrderTables)之间的连接语法,并将其更改为表达式:

invoiceData = 
    from x in context.PdtDeliveryTables 
    from odr in context.OrderTables.DefaultIfEmpty() 
    where (odr.OrderID != 0 && x.OrderID == x.OrderNo) 
          || x.OrderID == x.DeliveryNo)
    join pt in context.DiagramNoTables on x.DiagramID equals pt.DiagramID
    join mt in context.MaterialTables on pt.MaterialID equals mt.MaterialID 
    into t 
    from ct in t.DefaultIfEmpty()
    where (pt.CustomerID == customerID) 
           && (x.DeliveryDate >= (startDate ?? DateTime.MinValue) 
               && x.DeliveryDate <= (endDate ?? DateTime.MaxValue))
    orderby x.DeliveryDate  ascending
    select new InvoiceTable
        {    
        };