我试图做到这一点,但只有我第一次在查询运行时,习惯条款。
这需要对面向.NET 3.5,因此在WhereIf 4.0是不可用的。
var query =
from tb in dataContext.TableOne
where tb.DateTimeCreated >= fromDate &&
tb.DateTimeCreated <= toDate.AddDays(1)
select tb;
if (!string.IsNullOrEmpty(reference))
{
query.Where(tb => tb.Reference = reference));
}
if (!string.IsNullOrEmpty(reference))
query = query.Where(tb => tb.Reference = reference));
尝试
只要做到这一点在“一”
var query = (from tb in dataContext.TableOne
where (tb.DateTimeCreated >= fromDate && tb.DateTimeCreated <= toDate.AddDays(1)) && (string.IsNullOrEmpty(reference) || tb.Reference == reference)
select tb
);
或者说艾夫斯,这样做:
query = query.Where(...)