tags:

views:

53

answers:

1

Dear all,

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  A: 

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.

Matthew Abbott
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!