1 year ago

#362112

test-img

fossil

Entity Framework Core 6 - Date Comparision

I need to create a query to find records between a date range. I need to truncate the time for comparison. I came across a few threads suggesting to truncate time using DbFunctions.TruncateTime when comparing the dates.

I cannot find DbFunctions class in EntityFrameworkCore 6.0. How do I truncate time and compare the DateTime columns?

Edit: Excerpt from my query handler

IQueryable<TestReportItem> query = _context.TestReportItems;
DateTime requestDateStart = DateTimeOffset.FromUnixTimeMilliseconds(request.TestDateStart).LocalDateTime;
DateTime requestDateEnd = DateTimeOffset.FromUnixTimeMilliseconds(request.TestDateEnd).LocalDateTime;

if (request.FacilityId != 0)
{
    query = query.Where(e => e.FacilityId == request.FacilityId);
}

if (request.TestDateStart != 0 && request.TestDateEnd != 0)
{
    query.Where(e => e.TestDate.Date >= requestDateStart
        && e.TestDate.Date <= requestDateEnd);
}

var count = query.Count();

return count;

The request will be for a date range (dates converted into UnixTimeMilliseconds). I am using SqLite database.

asp.net-core

entity-framework-core

ef-core-6.0

0 Answers

Your Answer

Accepted video resources