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 the
that was successful but produced a lengthy date string like
May 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.
Unable to cast the type 'System.Nullable``1[[System.DateTime, mscorlib, Version=188.8.131.52, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
even with an exact date and time, doesn't locate any matches.
All of a sudden, magic works.
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.