[英]Linq to SQL Left Join with where clause looking for condition or null
我需要从 2 个表中选择记录,并使用左连接。 那部分很好。 但是,在 where 子句中,我需要选择where t2.fromDate > 20190101 OR t2.fromDate is null
。
问题是在 c# 中, t2.fromDate
是一个int
,而不是一个可为空的 int 。 我应该如何将t2.fromDate
与 null 进行比较? 我试过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 || ???
两种可能:
如果fp.OrderedDimDateKey
在数据库中可以为空,它在实体中也应该可以为空。
(你的情况,我猜)如果fp.OrderedDimDateKey
在数据库中不可为空,那么在查询中它为空的唯一方法是没有匹配的t2
实体。 这意味着测试可以作为
where fp.OrderedDimDateKey >= FromDate || fp == null
答案比我预期的要容易。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.