Reputation: 505
i have a problem in in Linq to entity when selecting this is my code
i want to select with date and id
Login Lo = new Login();
DateTime CurrentDate = DateTime.Now;
Lo = db.Logins.Where(i => i.Emp_id == employee.id &&
DbFunctions.TruncateTime(i.WorkDay).Value.Date == DbFunctions.TruncateTime(CurrentDate).Value).FirstOrDefault();
it give me this Error :
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Upvotes: 1
Views: 1089
Reputation: 39326
To solve your problem you could do this:
DateTime CurrentDate = DateTime.Now.Date;
var result= db.Logins.Where(i => i.Emp_id == employee.id &&
DbFunctions.TruncateTime(i.WorkDay) == currentDate).FirstOrDefault();
Your issue is because the Linq to Entities provider can't translate the operations that are defined in the Date
property to a proper expression in SQL:
// Returns the date part of this DateTime. The resulting value
// corresponds to this DateTime with the time-of-day part set to
// zero (midnight).
//
public DateTime Date {
get {
Int64 ticks = InternalTicks;
return new DateTime((UInt64)(ticks - ticks % TicksPerDay) | InternalKind);
}
}
That's why my advice is, outside of your query, call Date
property from DateTime.Now
, this way you will get the current date without the time part, and in your query, you don't need to call Date
property, that's the function of DbFunctions.TruncateTime
method
Upvotes: 2