Linq to SQL Left Join with where子句查找条件或为null

马特·道迪

我需要从2个表中选择一个带有左联接的记录。那部分很好。但是,在where子句中,我需要选择where t2.fromDate > 20190101 OR t2.fromDate is null

问题是在c#中,t2.fromDate是一个int,而不是可为null的int。我应该如何比较t2.fromDatenull?我已经尝试过了t2.fromDate == 0,但是那行不通,因为在SQL中,该值为null,但是在C#中,期望该值为int。

int FromDate = 20190101;
var data = (from hi in DbContext.t1
        from fp in DbContext.t2.Where(x => x.DimHierarchyItemKey == hi.Key).DefaultIfEmpty()
        where fp.fromDate >= FromDate || ???
马特·道迪

答案比我预期的要容易。

int FromDate = 20190101;
var data = (from hi in DbContext.t1
        from fp in DbContext.t2
                .Where(x => x.DimHierarchyItemKey == hi.Key).DefaultIfEmpty()
                .Where(x => x.fromDate>= FromDate).DefaultIfEmpty()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章