Select 来自两个表的值在 LINQ 中使用“WHERE NOT IN”和 DISTINCT 到 Sql

Select values from two tables using “WHERE NOT IN” and DISTINCT in LINQ in Linq to Sql

我有两个 table,例如 CustomerVisitDetail,我需要在 [= 以下进行转换23=] 查询 进入 Linq to Sql

select *
from Customer
where id not in (select distinct CustomerId
                 from visitdetail
                 where VisitDate='2016-06-13' and SalesRepAccId=1 and
                       RouteId=10
                ) and
      RouteId = 10 and Active=1 and SalesRepAccId=1

NOT IN 子句的嵌套查询中使用 !Contains()

var customerVisitDetails = (from row in db.Visitdetail
                            where row.VisitDate == '<add obj for date comparison>'
                            && row.SalesRepAccId == 1 && row.RouteId == 10
                            select row.CustomerId).Distinct().ToList();

var output = (from c in db.Customer
              where !customerVisitDetails.Contains(c.Id)
              && c.RouteId == 10 && c.Active == 1 && c.SalesRepAccId == 1
              select c);

此外,这对 starting with LinQ

很有帮助