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
source share
1 answer

I think Object.Equals(row.StartDate, aDateTime) will do the trick.

About it on Google . For example, this post .

+8
source

Source: https://habr.com/ru/post/898994/


All Articles