Linq记录不在其他表

时间:2015-12-06 03:56:59

标签: c# asp.net linq webforms

我想知道如何选择那些不在另一个表中的记录。

我有一个查询显示一段时间内的所有股票

    var query = (from stk in ie.stocks
                         join s in ie.staffs on stk.stk_createby equals s.stf_id
                         where stk.stk_createdate >= startDate &&
                         stk.stk_createdate <= endDate
                         select new
                         {
                             StockID = stk.stk_id,
                             RefNo = stk.stk_ref_id,
                             Weight = stk.stk_weight,
                             Grade = stk.stk_grade,
                             Remark = stk.stk_remark,
                             StaffName = s.stf_name
                         }).ToList();

我还有另一个查询来显示所有交付的股票。

                var query2 = (from ol in ie.orderLists
                              join stk in ie.stocks on ol.ol_stockid equals stk.stk_id
                              join dl in ie.deliveries on ol.ol_dlyid equals dl.dly_id
                              join s in ie.staffs on stk.stk_createby equals s.stf_id
                              where dl.dly_delivery_date >= startDate &&
                              dl.dly_delivery_date <= endDate
                              select new
                              {
                                  StockID = stk.stk_id,
                                  RefN = stk.stk_ref_id,
                                  Weight = stk.stk_weight,
                                  Grade = stk.stk_grade,
                                  Remark = stk.stk_remark,
                                  StaffName = s.stf_name
                              }).ToList();

所以我想要的是显示未交付的剩余库存。如何排除query2中的所有股票?

1 个答案:

答案 0 :(得分:2)

尝试使用Except方法。

离)

var ret = query1.Except(query2);