Using the 'LIKE' operator inside a Lambda Expression

Topics: General
Feb 1, 2013 at 10:53 AM
In my Entity (Sale) has a type of DateTime column Date.

I used following lambda expression to get the latest TrNo from that table. But always it gives me Null. Because it also compares the Time part of the column. Actually I wanted to compare the Date part of the column.
db.Sales.OrderByDescending(O => O.Date).Where(O => O.Date == DateTime.Now ).Select(O => O.TrNo).FirstOrDefault();
Please help me to get the build the right code. (My db is MySQL)

Thanks!
Developer
Feb 1, 2013 at 4:26 PM
You need to use EDM TruncateTime canonical function. Take a look at this thread on stackoverflow: http://stackoverflow.com/questions/14444267/entity-framework-datetime-vs-sql-server-date. Note that in EF6 functions were moved to DbFunctions class.
Feb 1, 2013 at 5:13 PM
Error: "FUNCTION <project namespace>.TruncateTime does not exist"
Developer
Feb 1, 2013 at 5:41 PM
How did you use it?
Feb 1, 2013 at 5:44 PM
db.Sales.OrderByDescending(O => O.Date).Where(O => O.Date == EntityFunctions.TruncateTime(DateTime.Now)).Select(O => O.TrNo).FirstOrDefault();
Developer
Feb 1, 2013 at 7:32 PM
This might be provider specific. From what I can see DevArt MySql provider has TruncateTime function (http://blogs.devart.com/dotconnect/entity-framework-canonical-functions.html starting from v4). Do you mind showing the stack trace?