在where子句中使用COALESCE进行SQL查询。

问题描述 投票:0回答:2

我想把下面的sql查询转换成linq to sql(实体框架)。

  select A.*, B.* from TABLE1 A
                   left join TABLE2 B
                  on A.LocationLoadPositionId = B.FkLocationLoadPositionId
                  where COALESCE(B.UploadStatus,0) = 0 

到目前为止,我已经做到了这一步。

var positions = (from a in dbContext.TABLE1 join b in dbContext.TABLE2
                   on a.LocationLoadPositionId equals b.FkLocationLoadPositionId into c from d in c.DefaultIfEmpty()
                     where d.UploadStatus == false select new { a, d }).ToList();

上面的linq查询似乎不能正常工作 因为我的where条件... ... 我在上面的两个查询中得到了不同的结果集......我在这里遗漏了什么?

entity-framework linq-to-sql
2个回答
1
投票

试试这个。

var positions = (from a in dbContext.TABLE1 join b in dbContext.TABLE2
                   on a.LocationLoadPositionId equals b.FkLocationLoadPositionId into c from d in c.DefaultIfEmpty()
                     where d.UploadStatus == false || d == null select new { a, d }).ToList();

0
投票

像这样...

var positions = (from a in dbContext.TABLE1
                 join b in dbContext.TABLE2
                 on a.LocationLoadPositionId equals b.FkLocationLoadPositionId into c
                 let x = d.UploadStatus == 0 //<--- COALESCE
                 from d in c.DefaultIfEmpty()
                 where x == true).ToList();
return query.ToList();
© www.soinside.com 2019 - 2024. All rights reserved.