79631093

Date: 2025-05-20 20:04:24
Score: 1
Natty:
Report link

The answer from Charlieface is technically the most correct answer as it actually uses ef core. However, on my database that solution was very slow and the query eventually timed out. I will leave this as an alternative for others who have slow/large databases.

For my user case I ended up doing a Context.Database.SqlQuery<T>($"QUERY_HERE").ToList(). This allows you to run sql directly and enables comparing an int32 to an object column on the database side - if the types dont match sql server will just omit that row, unlike ef core where an exception is thrown.

If necessary, the query can be broken up into 2 parts, one where you "find" the record you are looking for, and then the second part runs the "real query" with whatever key you found in the first part.

More on SqlQuery<T>:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.sqlquery?view=efcore-7.0

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Plaje