question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query: Compiled Queries: Support "non-rooted" query expressions

See original GitHub issue

User compiled queries allow for more expressiveness in creating query LINQ expressions. E.g.

var query = EF.CompileQuery(
                (NorthwindContext context)
                    => context.Customers.OrderBy(c => c.CustomerID).Select(c => c.CustomerID).FirstOrDefault() 
                        + context.Orders.OrderBy(o => o.CustomerID).Select(o => o.CustomerID).FirstOrDefault());

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
maumarcommented, Sep 17, 2020

Exception:

The 'InitializeStateManager' method has been called multiple times on the current query context. This method is intended to be called only once before query enumeration starts.
  Stack Trace: 
    QueryContext.InitializeStateManager(Boolean standAlone) line 137
    Enumerator.InitializeReader(DbContext _, Boolean result) line 217
    ExecutionStrategy.ExecuteImplementation[TState,TResult](Func`3 operation, Func`3 verifySucceeded, TState state) line 173
    ExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) line 160
    Enumerator.MoveNext()
1reaction
jnm2commented, Aug 2, 2019

This:

EF.CompileQuery<MyDataContext, int, ContainerBase>((ctx, contId) =>
    ctx.Container.AddIncludes().FirstOrDefault(d => d.Id == contId));

Is sugar for:

(oops, I did (queryable, contId) => queryable.AddIncludes().FirstOrDefault(d => d.Id == contId) instead, but you get the point)

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 AddIncludes is referenced above. In order for the calls inside your AddIncludes method to become available, Entity Framework would have to somehow decompile your AddIncludes method and construct an expression which represents what it does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SWQL queries not working in Custom ...
SWQL queries not working in Custom Query and Custom Reports since NPM v12 but still working in ... ON appl.id = comp.applicationid inner...
Read more >
How to combine series of two criteria queries into single ...
I have the following JPQL query which I need to convert to criteria query: SELECT new Tag(t.id, t.name) FROM GiftCertificate gc JOIN gc.tags ......
Read more >
Combined non-Nested and Nested Query in Elasticsearch
This assumes you are using the actual Nested documents, and not inner objects. For inner objects you can just use fully qualified paths...
Read more >
Advanced Query Functions
A JOIN is the method used to combine data from two different tables into one set of results. For example, querying the JOB....
Read more >
Combine multiple queries (Power Query)
Power Query provides an intuitive user interface for combining multiple queries within your Excel workbook by merging or appending them.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found