Linq to SQL: Where clause comparing Nullable <DateTime> with null SQL datetime column
Using C #, Linq to SQL, SQL Server, I have the following:
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 request does not do what I expected. In particular, if aDateTime is null, it asks StartDate = null
, not StartDate IS null
, which does not find a record where the column value is null. Change it to:
where aDateTime.HasValue ? (row.StartDate == aDateTime) : (row.StartDate == null)
It works, but annoying. Is there a simpler solution to this problem?
+6