-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: Compiled Queries: Support "non-rooted" query expressions #7016
Comments
I have a situation that looks like it fit in this issue.
Which I would call in multiple compiled queries looking like:
I get this exception:
But doing it directly in the compiled query works:
|
@Dunge - Your issue is not same as reported issue here. |
@smitpatel - Thank you. I'm sorry if the context doesn't match the original issue, I tried to search for a similar one not to create pollution, but I guess I just made things worse instead. So there's no way to create a part of reusable expression tree inside lambda queries? |
Yes, that is how compiler works and generates expression tree. Nothing EF Core can do about it. |
It may be helpful to show how moving IQueryable operations behind a method like that in an expression could be a dead end based on how C# and EF work. Methods mentioned in expressions are not executed prior to translation:
If Entity Framework had to execute AddIncludes before translating to a SQL query, it would also have to execute the whole thing. |
This: EF.CompileQuery<MyDataContext, int, ContainerBase>((ctx, contId) =>
ctx.Container.AddIncludes().FirstOrDefault(d => d.Id == contId)); Is sugar for: (oops, I did var queryableParameter = Expression.Parameter(typeof(IQueryable<ContainerBase>), "queryable");
var idParameter = Expression.Parameter(typeof(int), "id");
var dParameter = Expression.Parameter(typeof(ContainerBase), "d");
var expression = Expression.Lambda<Func<IQueryable<ContainerBase>, int, ContainerBase>>(
Expression.Call(
typeof(Queryable),
nameof(Queryable.FirstOrDefault),
typeArguments: new[] { typeof(ContainerBase) },
new Expression[]
{
Expression.Call(typeof(YourClass).GetMethod("AddIncludes"), new[] { queryableParameter }),
Expression.Quote(Expression.Lambda<Func<ContainerBase, bool>>(
Expression.Equal(Expression.Property(dParameter, "Id"), idParameter),
new[] { dParameter }))
}),
new[] { queryableParameter, idParameter });
EF.CompileQuery(expression); Note how |
This could be working in 3.1 release already. |
Exception:
|
Note to implementer: Remove the exception and make it no-op. |
Also fixes #7016: Support "non-rooted" query expressions in compiled queries.
User compiled queries allow for more expressiveness in creating query LINQ expressions. E.g.
This particular query currently fails, I think because of a re-linq issue. In general, we should decide whether we want to spend time on making this work and testing it.
The text was updated successfully, but these errors were encountered: