【问题标题】:How to use Left join with or condition on two fields?如何在两个字段上使用左连接或条件?
【发布时间】:2013-12-23 07:04:37
【问题描述】:
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
                };                       
            }
    

【问题讨论】:

  • @Soner 感谢编辑...!!!

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


【解决方案1】:

我删除了 x (PdtDeliveryTables) 和 odr (OrderTables) 之间的连接语法,并在 where 表达式上进行了更改:

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
        {    
        }; 

【讨论】:

  • 感谢您的回复,但查询将出现异常,我认为它正在进行无限超时异常。
  • 我已经在上面更新的代码中给出了更改后的查询,但我得到了超时异常。
  • 尝试限制查询结果,确保查询有效,例如在末尾添加First()。
猜你喜欢
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
相关资源
最近更新 更多