【发布时间】:2016-12-08 19:38:07
【问题描述】:
我尝试使用 except 方法。我认为这种方法更强大,并且比 WHERE 子句生成更清晰的 SQL,但不是。它生成相同的sql。
那么为什么/什么时候使用Except方法呢?
编辑:这是一个示例:
// Get customers except those which ID are in the LostCustomers table
TblCustomers.Except(TblCustomers.Where(tj => LostCustomers.Select(lj => lj.CustomerId).Contains(tj.CustomerID))).Select(j => new
{
CustomerId = j.CustomerID,
CustomerRef = j.CustomerRef,
CustomerName = j.Name
})
// Get customers except those which ID are in the LostCustomers table
TblCustomers.Where(tj => !LostCustomers.Select(lj => lj.CustomerId).Contains(tj.CustomerID)).Select(j => new
{
CustomerId = j.CustomerID,
CustomerRef = j.CustomerRef,
CustomerName = j.Name
})
谢谢
【问题讨论】:
-
这两种方法完全不同。您是在谈论具有特定条件的
Where吗? -
您能否举例说明如何使用每个 SQL 获得相同的 SQL。
标签: c# linq linq-to-entities where except