Efcore.pg: Adding LocalDate and Period fails because of inference issues

Created on 4 Sep 2019  路  5Comments  路  Source: npgsql/efcore.pg

Currently having something like that:

oc.Order.ActivationDate.Plus(Period.FromMonths(1))

or
oc.Order.ActivationDate.PlusMonths(1)

will "warn"(fail in 3.0) with:

The LINQ expression 'where ([oc].ActivationDate.Plus(__FromMonths_3) >= __nowLocalDate_4)' could not be translated and will be evaluated locally.

it would be great if that would be supported since DateTime.Add is supported aswell.

bug

All 5 comments

In 2.2, you should be able to write oc.Order.ActivationDate + Period.FromMonths(1) which is even nicer.

In 3.0 this doesn't work because of type inference issues, will use this issue to track that.

well that is really helpful. Unfortunatly the code I would've needed would have looked like that:
new LocalDate(oc.Order.ActivationDate.Year, oc.Order.ActivationDate.Month, 15) + Period.FromMonths(1)

but new LocalDate can't be translated, too. (with recent postgres versions 9.5+ Npgsql could translate it to make_date I guess)

So in the end I created a DbFunction that will do what I wanted.
If you think that's worth another issue aswell, I would create one.
(I think I should also look into the source code, maybe I can try to fix it by myself)

@schmitch it's really nice to see people using the NodaTime plugin and trying to translate complex expressions.

In general, there is a lot of stuff in NodaTime (and in general!) that could be translated but isn't - we usually wait until people request translations before going too far into it. We should probably have an epic issue listing all NodaTime translations we'd like to see.

In general, writing method/member translators is quite easy (and also very satisfying), but you're requesting a constructor translator, which is unfortunately going to be more complex. Please open another issue requesting this and I'll try to take a look for 3.0.

Fixed the type inference issue that was blocking the addition operator. @schmitch if you want, you can open a new issue for the constructor translation.

Thank you, I created the Issue #1020. Guess it's a nice addition in the future. I think if one constructor translation is inside the codebase it would be easier for other people to add the other ones.

Was this page helpful?
0 / 5 - 0 ratings