在加入想要得到不匹配的记录

时间:2013-09-28 11:34:37

标签: linq entity-framework join

使用SalSalesOrderFinancial表加入SalSalesOrderDetail上的DF 4与2010.Face问题。

enter image description here

在SalSalesOrderFinancial中,一条记录的SalesOrderDetailID = null。想要获得SalSalesOrderFinancial中不存在SalesOrderDetailID的记录。

为了得到理想的输出写下来linq语法,它正在工作。寻找更好的连接语法。有任何方法可以在一个连接中获得所需。

      var tempBDwithSODetail = (from p in this.Context.SalSalesOrderFinancials
                                          where p.SalesOrderDetailID != null
                                          select p.SalesOrderDetailID).AsEnumerable();

                var tempBDwithOutSODetail = (from p in this.Context.SalSalesOrderFinancials where p.SalesOrderDetailID == null select p).AsEnumerable();
                var querySOD = (this.Context.SalSalesOrderDetails.Where(item => !tempBDwithSODetail.Contains(item.SalesOrderDetailID))).AsEnumerable();

var tempBDetail = (from p in querySOD
                  join q in tempBDwithOutSODetail on p.SalesOrderID equals q.SalesOrderID
                                   where q.SalesOrderDetailID == null
                                   select new
                                   {
                                       q.SalesOrderID,
                                       p.SalesOrderDetailID,
                                       q.CurrencyID,
                                       q.BillingPolicyID,
                                       q.BillTypeID,
                                       q.BillingTypeID
                                   }).AsEnumerable();

如果有任何疑问,请询问。谢谢你。

1 个答案:

答案 0 :(得分:0)

如果我找到你,你只需在SalSalesOrderDetailSalSalesOrderFinancial之间进行简单的左连接......

var query = (from u in this.Context.SalSalesOrderDetail
             join t in this.Context.SalSalesOrderFinancials 
                 on u.SalesOrderDetailID equals t.SalesOrderDetailID into JoinedList
             from t in JoinedList.DefaultIfEmpty()
             select new
             {
                 SalSalesOrderDetail = t,
                 SalSalesOrderFinancials = u
             })
             .Where(u => u.SalSalesOrderFinancials == null)
             .ToList();