How can I compare a nullable type date using user entered date?

c# datetime entity-framework entity-framework-6


I can't figure out how to make a comparison between a user-entered date and a date in a table made by EF6. I've tried a number of solutions that build but fail when executed while searching the database using LINQ Extensions.

a user's input

DateTime userdate;
DateTime.TryParse(search.Value, out userdate);

Aims made so far:

a = dbContext.Messages.Where(x => x.Date.Date == userdate.Date); // doesn't compile
a = dbContext.Messages.Where(x => x.Date.Value.Date == userdate.Date); // error # 4
a = dbContext.Messages.Where(x => x.Date.ToString() == userdate.Date.ToString()); // issue #2
a = dbContext.Messages.Where(x => x.Date.Value.ToString() == userdate.Date.ToString()); // issue #2
a = dbContext.Messages.Where(x => Object.Equals(x.Date, userdate.Date)); // throws error #1
a = dbContext.Messages.Where(x => Object.Equals(x.Date.Date, userdate.Date)); // throws error #1

I've tried googling for this a million times and always come up with solutions that build but always fail to execute.

One of the versions of theToString() that was successful but produced a lengthy date string likeMay 12, 2005 00:00:00 Additionally, it appears like there would be an excessive amount of overhead while searching through millions of entries if I tried to convert my userdate into a long date for comparison.

I've run out of ideas right now.


  1. Unable to cast the type 'System.Nullable``1[[System.DateTime, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.

  2. even with an exact date and time, doesn't locate any matches.

  3. All of a sudden, magic works.

  4. The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

last response I discovered that the SqlFunctions class has based on the specified response. the T-SQL DateDiff function's mapping to the DateDiff method. I used it to calculate the difference in days between the two dates and only kept those that returned a value of 0. Because of this, I was able to compare the dates without having to perform any SQL-side conversions.

3/29/2015 6:32:35 PM

Accepted Answer

3/29/2015 4:35:46 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow