2

I have a helper method used a lot in my queries. Sample below:

public static bool HelpMe()
    => dbContext.MyTable.Any(entity => entity.SomeBoolean)

The problem is when I use it in another query, I get another that EF cannot translate it.

In short, this won't work:

dbContext.OtherTable.Where(entity => HelpMe() == entity.Value)

but this will:

dbContext.OtherTable.Where(entity => dbContext.MyTable.Any(entity => someCondition(entity)) == entity.Value)

Obviously the 2nd is bad because It's not as readable and requires changing the code in multiple places.

I've read about IMethodCallTranslator and am wondering how it can be used to get EF core to convert the HelpMe method just as it normally would.

neyisa1971
  • 43
  • 1
  • 3
  • @CodeCaster do you have any documentaton on how to do that? – neyisa1971 Oct 12 '20 at 06:50
  • Expression visitors/method call translators are not enough because they cannot extract the content (source code) of a method at runtime. In order to do that they would need a decomplie capability. Here https://stackoverflow.com/questions/62115690/ef-core-queries-all-columns-in-sql-when-mapping-to-object-in-select/62138200#62138200 is an example of using 3rd party package for that and plugging it into EFC 3.x – Ivan Stoev Oct 12 '20 at 06:53
  • Maybe the attribute `[MethodImpl(MethodImplOptions.AggressiveInlining)]` (on the helper) could help? – Pac0 Oct 12 '20 at 12:14

0 Answers0