可选 Where in Linq-to-SQL 查询

Optional Where in Linq-to-SQL query

仅当 DateTime? start 不为 null 时,我才尝试过滤 table,但出现异常,我触摸了 .Value 的 null 对象。实现此目标的正确方法是什么?

.Where(e => start.HasValue ? e.Timestamp >= start.Value : true)

对不起,伙计们,我自己找到了答案。这个工作正常:

.Where(e => !start.HasValue || e.Timestamp >= start)

我查看了 SQL Profiler -- 当 start 为空时,Linq-to-SQL 完全删除了 Where