Telerik OpenAccess generating the same query for every function call

93 Views Asked by At

I'm using a Generic Repository like pattern to fetch data. There are 100+ entities so creating a separate repository for each is not really an option. here are a few functions from the same class:

    public int Count(Func<TEntity, bool> x=null)
    {
        return x == null ?
            mgr.CTX.GetAll<TEntity>().Count() :
            mgr.CTX.GetAll<TEntity>().Where(x).Count();
    }

    public TEntity One(Func<TEntity, bool> x)
    {
        return mgr.CTX.GetAll<TEntity>().Where(x).Take(1).FirstOrDefault();
    }

    public IQueryable<TEntity> All(Func<TEntity, bool> x=null)
    {
        return x == null ?
            mgr.CTX.GetAll<TEntity>() :
            mgr.CTX.GetAll<TEntity>().Where(x).AsQueryable<TEntity>();
    }

The problem is no matter which function is call, the Sql profiler shows the same

Select [columns] from [table]

I suppose when using Take(1) or Count() or Where() the query should be made accordingly using Count(), Top or Where clauses of Select but these functions have absolutely no effects on query generation. Apparently, every operation seems to be performed in memory after fetching all the data from server.

Guide me if there is something wrong with the way I'm accessing it or this is the normal behavior of telerik?

1

There are 1 best solutions below

1
On BEST ANSWER

I believe you are victim of a subtle difference between definitions of LINQ extension method - in-memory ones use Func<> while SQL-bound use Expression<> as parameter type.
My suggestion is to change All(Func<TEntity, bool> x=null) to All(Expression<Func<TEntity, bool>> x=null)