hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

How to use DelegateDecompiler with EF6 and OData V4 #144

Open boosterch opened 5 years ago

boosterch commented 5 years ago

Hope you can help me with configuring the DelegateCompiler with EF6 and OData V4. As I understand it is necessary to add Decompile at the end of the query. I would like to add this to my ODataController, but then the Decompile is at the beginning.

        [EnableQueryAutoMapped]
        //[EnableQuery]
        public virtual IQueryable<TEntity> Get()
        {
            var dbSet = Db.Set<TEntity>().AsNoTracking(); //.Decompile();
            return dbSet;
        }

Another solution is to override the EnableQueryAttribute and implement ApplyQuery. However I do not see a way to call Decompile on the none generic IQueryable. Using reflection seems to work, but is maybe not the right solution.

        public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
        {
            var query = base.ApplyQuery(queryable, queryOptions);
            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
            CultureInfo culture = null; // use InvariantCulture or other if you prefer
            var provider = (DecompiledQueryProvider)
            Activator.CreateInstance(typeof(DecompiledQueryProvider), flags, null, new[] { query.Provider }, culture);
            return provider.CreateQuery(query.Expression);
        }

Hope you can point me in the right direction. Thanks.

edgarabrahamyanpayfactors commented 4 years ago

Hi @boosterch do you have any progress? This solution works for me when I have the following query $select=Id,FullName&$filter=contains(FullName,'Test') Full name is computed property. But as soon as I add $count=true I'm getting the following error "message": "Invalid column name 'FullName'.", It looks like IQueryable ApplyQuery doesn't being called for $count because I see SQL profiler that EF trying to query FullName column. I'm using EF Core v2.0 Microsoft.AspNet.OData v7.2.1, DelegateDecompiler v0.26.1. Thanks