简体   繁体   中英

How i can make dynamic lambda Expression from string?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM