npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Translate range construction, e.g. NpgsqlRange<DateTime> to daterange()

tsanton opened this issue · comments

I've got DB-context that I need to do some date range filters against.

Unfortunately I'm not able to alter the context, and currently it contains distinct date columns named start_dateand end_date.
I know that if I could alter the model I could declare a public NpgsqlRange<DateTime> Period column in the entity I could apply a query predicate as where myEntity.Period.Overlaps(new NpgsqlRange<DateTime>(x, y)).

The problem I'm having is that I'm not able to alter the model, and I'm getting internal server errors when trying to new up the left hand side of the predicate (rather than defining it in the model):

`where new NpgsqlRange<DateTime>(myEntity.StartDate, myEntity.EndDate).Overlaps(new NpgsqlRange<DateTime>(x, y))`.

Is there any way I can apply date range predicated (AND daterange(start_date, end_date, '[]') && daterange(@p3, @p4, '[]') without altering the entity?

@roji sorry to bother you but I'm hoping you can spare me a few minutes of your time and either confirm it's not possible or maybe point me in the right direction?

Have a great weekend when that time comes!

/T

The provider currently doesn't translate new NpgsqlRange<DateTime> (or provide another way to create a range in SQL) - that's definitely something that can be added. But isn't it easier it to just compare start to start and end to end directly, rather than constructing ranges and doing overlap over them?

If you really need ranges for some reason, you may be able to use SQL for that particular part of the query. Another option would be to add a generated column that creates the range over the two other columns, and then you're back to simply using the regular range operations; that can allow having the range in the database (as a computed/generated column) without removing/changing the existing columns/model.