首页 > 解决方案 > 在 where 子句中使用 COALESCE 的 Linq to SQL 查询

问题描述

我正在尝试将以下 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();

由于我的 where 条件,上面的 linq 查询似乎无法正常工作......我为上面的两个查询得到不同的结果集......我在这里错过了什么?......

标签: entity-frameworklinq-to-sql

解决方案


尝试这个:

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();

推荐阅读