Linq to SQL:比较 Nullable的Where 子句带有 SQL 日期时间空列
使用 C#、Linq to SQL、SQL Server,我有这样的情况:
MyTable has a column with a "datetime null" column.
DateTime? aDateTime;
var records = (from row in MyTable where row.StartDate == aDateTime);
我注意到查询没有达到我的预期。具体来说,如果 aDateTime 为 null,则查询 StartDate = null
而不是 StartDate IS null
,后者不会查找列值为 null 的记录。将其更改为:
where aDateTime.HasValue ? (row.StartDate == aDateTime) : (row.StartDate == null)
有效但很烦人。有没有更简单的解决方案来解决这个问题?
Using C#, Linq to SQL, SQL Server, I have this:
MyTable has a column with a "datetime null" column.
DateTime? aDateTime;
var records = (from row in MyTable where row.StartDate == aDateTime);
And I noticed that the query does not do what I expected. Specifically, if aDateTime is null, it queries StartDate = null
rather than StartDate IS null
, which does not find records where the column value is null. Changing it to:
where aDateTime.HasValue ? (row.StartDate == aDateTime) : (row.StartDate == null)
works but is annoying. Is there a simpler solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
Object.Equals(row.StartDate, aDateTime)
可以解决问题。Google 上有很多相关内容。例如,这篇文章< /a>.
I think
Object.Equals(row.StartDate, aDateTime)
will do the trick.There is lots about this on Google. For example, this post.