我正在尝试完成此操作,但只有在查询运行时才使用我的第一个 where 子句。
这需要用于 .Net 3.5,因此 4.0 中的 WhereIf 不可用。
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));
}