C
C#10mo ago
Rese

❔ How can I make a function of a simple condition EF Core translatable

Hey, so I have this function, but it seems to throw an error by not being translatable by EF Core, even though if the code is extracted from the function, it seems to work just fine. Function that cannot be translated:
public static bool IsWithinRange(this DateTime target, DateTime rangeStart, DateTime rangeEnd)
=> target >= rangeStart && target <= rangeEnd;
public static bool IsWithinRange(this DateTime target, DateTime rangeStart, DateTime rangeEnd)
=> target >= rangeStart && target <= rangeEnd;
But this is fine:
.Where(x => x.Timestamp >= _startDate && x.Timestamp <= _endDate)
.Where(x => x.Timestamp >= _startDate && x.Timestamp <= _endDate)
Can anyone explain how I can fix that?
16 Replies
Pobiega
Pobiega10mo ago
Yep! Functions are not translatable to SQL, but expression are check the types on Where there
Rese
Rese10mo ago
Okay, I did try to form it into an expression and it also didn't seem to work. I'm assuming I'm just calling it wrong since there are multiple parameters. Expression:
public Expression<Func<DateTime, DateTime, DateTime, bool>> IsWithinRangeExpr =
(target, rangeStart, rangeEnd) => target >= rangeStart && target <= rangeEnd;
public Expression<Func<DateTime, DateTime, DateTime, bool>> IsWithinRangeExpr =
(target, rangeStart, rangeEnd) => target >= rangeStart && target <= rangeEnd;
Call:
.Where(x => IsWithinRangeExpr.Compile()(x.Timestamp, _startDate, _endDate))
.Where(x => IsWithinRangeExpr.Compile()(x.Timestamp, _startDate, _endDate))
Throws the same error As in not translatable Or is it that I can't call compile and therefore use those other arguments?
Pobiega
Pobiega10mo ago
that compile is definately not right hang on a sec
Rese
Rese10mo ago
yeah it didn't look right to me either 😄
Pobiega
Pobiega10mo ago
I'd probably recommend just not using a function and baking it into your query. It will be a lot more readable that way :p
Rese
Rese10mo ago
The reason why I wanna do it this way is because it's part of a more complicated query, which would just clutter the code a lot. But that one is definitely possible to be translatable by EF Core once this issue is resolved
Pobiega
Pobiega10mo ago
you can make a dynamic expression, but it might be a bit messy
Rese
Rese10mo ago
what does that mean? Like wasn't the above example that?
Pobiega
Pobiega10mo ago
something like this
Pobiega
Pobiega10mo ago
Stack Overflow
Build Expression to filter data EF Core
I need to reuse available expression: Expression<Func<Picture, int>> selector = o => o.EntityId; And build expression for Where: Expression<Func<Picture, bool>> filter...
Rese
Rese10mo ago
Well that just looks miserable 😄
Pobiega
Pobiega10mo ago
exactly
Rese
Rese10mo ago
So there's no other way? Essentially EF Core can't parse the method itself even if it is a function with content that would be translatable?
Pobiega
Pobiega10mo ago
Thats correct. EF can only translate expressions There might be a better way to make that expression method, but I'm far from an expert at this
Angius
Angius10mo ago
The expression has to be the same as the lambda the method takes So if it's a Things.Where(x => x.Stuff > 10) you need
Expression<Func<Thing, bool>> Boo = (Thing t) => t.Stuff > 10;
Expression<Func<Thing, bool>> Boo = (Thing t) => t.Stuff > 10;
and you can call
Things.Where(Boo)
Things.Where(Boo)
So an expression like
public Expression<Func<Thing, bool>> IsWithinRangeExpr
= (t) => t.Target >= t.RangeStart && t.Target <= t.RangeEnd;
public Expression<Func<Thing, bool>> IsWithinRangeExpr
= (t) => t.Target >= t.RangeStart && t.Target <= t.RangeEnd;
might work
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.