❔ 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:
But this is fine:
Can anyone explain how I can fix that?
16 Replies
Yep! Functions are not translatable to SQL, but expression are
check the types on
Where
thereOkay, 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:
Call:
Throws the same error
As in not translatable
Or is it that I can't call compile and therefore use those other arguments?
that compile is definately not right
hang on a sec
yeah it didn't look right to me either 😄
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
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
you can make a dynamic expression, but it might be a bit messy
what does that mean?
Like wasn't the above example that?
something like this
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...
Well that just looks miserable 😄
exactly
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?
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
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
and you can call
So an expression like
might workWas 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.