VisualStudioFeeds

All Visual Studio blogs in one place

AddThis Social Bookmark Button
public static class QueryableExtensions     {         public static IQueryable Filter(this IQueryable queryable, IEnumerable> filters) where T : class         {             foreach (var filter in filters)             {                 var propertyName = typeof(T).GetProperties().Select(x => x.Name).SingleOrDefault(x => x.ToLower() == filter.Key.ToLower());                 if (propertyName.IsNullOrWhiteSpace()) continue;                 var parameterExpression = Expression.Parameter(typeof(T));                 var propertyExpression = Expression.Property(parameterExpression, propertyName);                 var toStringCallExpression = Expression.Call(propertyExpression, typeof(T).GetMethod("ToString"));                 var toLowerCallExpression = Expression.Call(toStringCallExpression, typeof(string).GetMethod("ToLower", Type.EmptyTypes));                 var constantExpression = Expression.Constant(filter.Value.ToLower());                 var equalExpression = Expression.Equal(toLowerCallExpression, constantExpression);                 var methodCallExpression = Expression.Call(typeof(Queryable), "Where", new[] { typeof(T) }, queryable.Expression, Expression.Lambda>(equalExpression, new[] { parameterExpression }));                 queryable = queryable.Provider.CreateQuery(methodCallExpression) as IQueryable;             }             return queryable;         }     }

Search

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : .Nettuce : IQueryable Dynamic Expressions