Reputation: 1322
I am trying to compare dates in Entity Framework 6 LINQ Query.
var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)==0).ToList();
However its resulting in Exception:
"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
I just want to compare dates by trimming time part.
Any suggestions how this can be done?
Upvotes: 1
Views: 3030
Reputation: 1086
Linq can't translate some native .Net code into Sql so try to use DateTime.Now function and other .Net function outside Linq query and then reference it in query.
String currentTime = DateTime.Now.ToShortDateString();
var bookings = context.Bookings.Include("BookingItems").Where(x=>x.IsDeleted == false && DateTime.Compare((x.BookingDate).Value.Date,currentTime)==0).ToList();
I haven't run though there might be some syntax error, but try anyway
Upvotes: 2
Reputation: 13599
Linq to sql or sql does not have implemination DateTime.Compare
Try converting to collection.
var bookings = context.Bookings.Include("BookingItems").ToList().Where(x=>x.IsDeleted == false && DateTime.Compare(DbFunctions.TruncateTime(x.BookingDate).Value,DateTime.Now.Date)==0).ToList();
Upvotes: 1