Im developing an app using ASP core 8 and EF core 8.
i have installed Npgsql.EntityFrameworkCore.PostgreSQL Version=8.0.2
.
i have a scenario where i need to filter an entity by date by applying a timezone. The date is in UTC. I have used the EF.Functions.AtTimezone
method in to convert time in MSSQL
when working with EF Core
. I tried to get the same/similar approach with Postgres with EF Core, but it seems like there is no built-in method for that in postgres with Ef Core.
So i googled and came up with this where it’s functioning as expected in postgres sql with EF Core
public async Task<(IReadOnlyList<AdminEventSummaryDto>, int)> GetEventListForDay(Paginator paginator, string timezoneName, DateTime date, Guid userId)
{
var dateString = date.ToString("yyyy-MM-dd");
var sqlQuery = $@"
SELECT *
FROM ""SN_ATS_Event""
WHERE (""EventStartDateTimeUtc"" AT TIME ZONE 'UTC' AT TIME ZONE '{timezoneName}')::date = '{dateString}'::date";
var query = _context.Events
.FromSqlRaw(sqlQuery)
.Where(w => w.InterViewers.Any(a => a.InterviewerId == userId));
var totalRecordCount = await query.CountAsync();
var list = await query.Select(e => new AdminEventSummaryDto()
{
// select what ever is needed
})
.Skip((paginator.PageNumber - 1) * paginator.PageSize)
.Take(paginator.PageSize)
.AsSplitQuery()
.ToListAsync();
return (list, totalRecordCount);
My problem is is this correct or is there a better way that i can apply the timezone i want without having to write a raw sql?