Is Dynamic LINQ still in use and appropriate for filtering search results?

asp.net-mvc-3 dynamic-linq filtering linq

Question

I am developing an ASP.NET MVC3 application in C#.

I am trying to implement in my application a "narrow-down" functionality applied the result-set obtained from a search.

In short, after I perform a search and the results displayed in the center of the page, I would like to have on the left/right side of the page a CheckBoxList helper for each property of the search result. The CheckBox of each CheckBoxList represent the distinct values of the property.

For instance if I search Product and it has a Color property with values blue, red and yellow, I create a CheckBoxList with text Color and three CheckBox-es one for each color.

After a research on the Web I found this Dynamic LINQ library made available by Scott Guthrie. Since the most recent example/tutorial I found is from 2009, I was wondering whether this library is actually good (and maintained) or not.

In the latter case is jQuery the best way to implement such functionality?

1
2
4/16/2012 10:05:28 PM

Popular Answer

You can solve it by building the needed predicate expressions dynamically, using purely .NET framework.

See code sample below. Depending on the criteria, this will filter on multiple properties. I've used IQuerable because this will enable both In-Memory as remote scenario's such as Entity Framework. If you're going with Entity Framework, you could also just build an EntitySQL string dynamically. I expect that will perform better.

There is a small portion of reflection involved (GetProperty). But this could be improved by performing caching inside the BuildPredicate method.

public class Item
{
    public string Color { get; set; }
    public int Value { get; set; }
    public string Category { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var list = new List<Item>()
        {
            new Item (){ Category = "Big", Color = "Blue", Value = 5 },
            new Item (){ Category = "Small", Color = "Red", Value = 5 },
            new Item (){ Category = "Big", Color = "Green", Value = 6 },
        };

        var criteria = new Dictionary<string, object>();
        criteria["Category"] = "Big";
        criteria["Value"] = 5;

        var query = DoDynamicWhere(list.AsQueryable(), criteria);
        var result = query.ToList();
    }

    static IQueryable<T> DoDynamicWhere<T>(IQueryable<T> list, Dictionary<string, object> criteria)
    {
        var temp = list;

        //create a predicate for each supplied criterium and filter on it.
        foreach (var key in criteria.Keys)
        {
            temp = temp.Where(BuildPredicate<T>(key, criteria[key]));
        }

        return temp;
    }

    //Create i.<prop> == <value> dynamically
    static Expression<Func<TType, bool>> BuildPredicate<TType>(string property, object value)
    {
        var itemParameter = Expression.Parameter(typeof(TType), "i");

        var expression = Expression.Lambda<Func<TType, bool>>(
            Expression.Equal(
                Expression.MakeMemberAccess(
                    itemParameter,
                    typeof(TType).GetProperty(property)),
                Expression.Constant(value)
            ),
            itemParameter);

        return expression;
    }
}
5
5/18/2012 6:49:49 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow