using DbFunctions = System.Data.Entity.DbFunctions;
Using the above namespace I tried below ways but nothing worked. This code is throwing an exception which states...
public async Task<int> SomeFunction(){
var count = await _context.Drives.CountAsync(c => DbFunctions.TruncateTime(c.CreatedOn) == DateTime.Today);
var count1 = await _context.Drives.Where(c => DbFunctions.TruncateTime(c.CreatedOn) == DateTime.Today).CountAsync();
var data = _context.Drives.Where(c => !c.IsDeleted).ToList();
//This throw an exception
// "This function can only be invoked from LINQ to Entities."
var count2 = data.Count(x=> DbFunctions.TruncateTime(c.CreatedOn) == DateTime.Today)
}
The LINQ expression 'DbSet().Where(d => DbFunctions.TruncateTime((Nullable)d.CreatedOn) == (Nullable)DateTime.Today)' could not be translated
Can someone help me out how can I compare two dates (only date not with time) in LINQ and Entity Framework?
The problem is, that you are applying
DbFunctions.TruncateTimetodata.datais of typeList<Drive>on notIQueryable<Drive>, because you already calledToList(). So youCountwould be evaluated in memory and not on the database.If that is really, what you want, then you can just use it like this:
If you want to invoke your query on the database, then you can use
Depending on the versions and database, you are using, solution 1 may also work on the database
Another solution would be to just use a time range, which will definetily work on the database and in memory
A final side-note: You should use
asyncandawaitwhen you query the database.