3

I need use Lambda Expression in my method

public static class QueryableDynamicExtension
{
    public static IQueryable<T> DynamicEquals<T>(
       this IQueryable<T> query,
       string field,
       object value)
    {
        Expression<Func<T, bool>> expr = ???                   

        return query.Where(expr);
    }
}

In this method, I want it return same as

IQueryable<Article> articles = new ModelDataContext().Articles.Where(m => m.CategoryId == 5);
// I want replace by
IQueryable<Article> articles = new ModelDataContext().Articles.DynamicEquals("CategoryId", 5);

How should I build the "expr" in this case? Please help.

1 Answer 1

2

You could look into the Dynamic LINQ library, as blogged by Scott Gu here. I've used this previously where I've built a rules-based product system for work, and have used dynamic expressions stored in our database layer to provide additional expressions to filter out product sets.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, I know too the Dynamic LINQ library, and it can resolve my problem, but it too complex, I only want use some Lambda Expression in my project. So that if possible you can show for me the way to resolve my problem. Thanks!

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.