5

I have a repository class which has a GetAsQueryable method defined as follows:

public class Repository<TEntity> : IDisposable, IRepository<TEntity> where TEntity : class
{
    internal DbSet<TEntity> _DbSet;

    public virtual IQueryable<TEntity> GetAsQueryable(
         Expression<Func<TEntity, bool>> filter = null,
         Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
         string includeProperties = "")
    {
        IQueryable<TEntity> query = _DbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query);
        }
        else
        {
            return query;
        }
    }
}

In my calling code I typically do operations such as:

IRepository<Tracking> repoTracking = new Repository<Tracking>(context);
IQueryable<Tracking> tracking = repoTracking.GetAsQueryable();
var results = tracking.Where(t => t.Status_Code_Id == 15).ToList();

This works great; however now I want to be able to build the lambda sent into the .Where at runtime. I have tried building an expression tree as follows:

IRepository<Tracking> repoTracking = new Repository<Tracking>(context);
IQueryable<Tracking> tracking = repoTracking.GetAsQueryable();
var results = tracking.Where(t => t.Status_Code_Id == 15).ToList();

IRepository<Tracking> repoTracking = new Repository<Tracking>(context);
IQueryable<Tracking> tracking = repoTracking.GetAsQueryable();

ParameterExpression pe = Expression.Parameter(typeof (int), "Status_Code_Id");
LambdaExpression lambda = Expression.Lambda(Expression.Equal(pe, Expression.Constant(15)));
MethodCallExpression whereExpression = Expression.Call(
    typeof(Queryable),
    "Where",
    new Type[] { tracking.ElementType },
    tracking.Expression,
    lambda);

However, this yields the following exception:

No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

Why can it not find the Where method on my instance of Tracking?

2 Answers 2

8

If you want to build t => t.Status_Code_Id == 15 dynamically you need don't need to call Where using Expression. Just create Expression<Func<TEntity, bool>> and pass it to Where:

ParameterExpression t = Expression.Parameter(typeof(Tracking), "t");
Expression statusCode = Expression.Property(t, "Status_Code_Id");
Expression comparison = Expression.Equal(statusCode, Expression.Constant(15));
Expression<Func<Tracking, bool>> lambda
    = Expression.Lambda<Func<Tracking, bool>>(comparison, t);
var results = tracking.Where(lambda).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

I was missing the Expression.Parameter(typeof(Tracking), "t"); piece. Being able to build the lambda to pass into the .Where method is exactly what I was looking to do! @MarcinJuraszek Dziękuję bardzo!
0

Remember that .Where() has a generic signature. You don't have to provide explicit generic parameters to it when you use a lambda expression because the compiler can infer the types for you, but when you're using reflection you'll need to create a generic signature to call based on the types that you're dealing with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.