C
C#16mo 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
Pobiega16mo ago
Yep! Functions are not translatable to SQL, but expression are check the types on Where there
Rese
ReseOP16mo 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
Pobiega16mo ago
that compile is definately not right hang on a sec
Rese
ReseOP16mo ago
yeah it didn't look right to me either 😄
Pobiega
Pobiega16mo 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
ReseOP16mo 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
Pobiega16mo ago
you can make a dynamic expression, but it might be a bit messy
Rese
ReseOP16mo ago
what does that mean? Like wasn't the above example that?
Pobiega
Pobiega16mo ago
something like this
Pobiega
Pobiega16mo 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
ReseOP16mo ago
Well that just looks miserable 😄
Pobiega
Pobiega16mo ago
exactly
Rese
ReseOP16mo 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
Pobiega16mo 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
Angius16mo 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
Accord16mo 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.
Want results from more Discord servers?
Add your server