DataView dvData = new DataView(data); dvData.RowFilter = “SomeField=’Test’”;
DataView dvData = new DataView(data); dvData.RowFilter = “SomeField LIKE ’Te%’”;
List<MyObject> objectResults = MyList.FindAdd(x => x.MyProperty.ToLower() == “Value”);
List<MyObject> objectResults = MyList.FindAdd(x => “MyPropertyName” == “Value”);
public static IEnumerable<T> DynamicFilter<T>(this IEnumerable<T> source, string propertyOrFieldName, string searchValue) { Func<T, bool> predicate = CreatePredicate<T>(propertyOrFieldName, searchValue); return source.Where(predicate); } static Func<T, bool> CreatePredicate<T>(string propertyOrFieldName, string searchValue) { var arg = Expression.Parameter(typeof(T), "arg"); var body = Expression.Equal(Expression.Property(arg, propertyOrFieldName), Expression.Constant(searchValue)); return Expression.Lambda<Func<T, bool>>(body, arg).Compile(); }
List<Employee> emps = Employee.LoadListCollection(); List<Employee> empList = emps.DynamicFilter("FirstName", "Angie").ToList();
public static IEnumerable<T> ContainsString<T>(this IEnumerable<T> data, string propertyOrFieldName, string searchValue) { var param = Expression.Parameter(typeof(T), "x"); var body = Expression.Call( typeof(BusinessObjectBase).GetMethod("Like", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), Expression.PropertyOrField(param, propertyOrFieldName), Expression.Constant(searchValue, typeof(string))); var lambda = Expression.Lambda<Func<T, bool>>(body, param); return data.Where(lambda.Compile()); }
/// <summary> /// comparison operator. Needed for dynamically filtering the objects /// </summary> /// <param name="a">A.</param> /// <param name="b">The b.</param> /// <returns></returns> static bool Like(string a, string b) { return a.ToLower().Contains(b.ToLower()); }
List<Employee> emps = Employee.LoadListCollection(); List<Employee> empList = emps.ContainsString("FirstName", "An").ToList();
var body = Expression.Call( typeof(BusinessObjectBase).GetMethod("Like", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), Became this:var body = Expression.Call( typeof(BusinessObjectBase).GetMethod(operatorName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), Now I could create two static operators (or as many as I might ever need) to provide this dynamic functionality.public static IEnumerable<T> RunOperator<T>(this IEnumerable<T> data, string operatorName, string propertyOrFieldName, string searchValue) { var param = Expression.Parameter(typeof(T), "x"); var body = Expression.Call( typeof(BusinessObjectBase).GetMethod(operatorName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), Expression.PropertyOrField(param, propertyOrFieldName), Expression.Constant(searchValue, typeof(string))); var lambda = Expression.Lambda<Func<T, bool>>(body, param); return data.Where(lambda.Compile()); } In my BusinessObjectBase class, I added the following methods (Like for wildcard searches, and Equals for a case-insensitive search): static bool Like(string a, string b) { return a.ToLower().Contains(b.ToLower()); } static bool Equals(string a, string b) { return a.ToLower() == b.ToLower(); } I can call both of these methods like this:public List<T> FilterData<T>(List<T> data, bool wildcardSearch) { List<T> results = data; if (wildcardSearch) results = results.RunOperator("Like", fi.DataField, fi.FilterValue()).ToList(); else results = results.RunOperator("Equals", fi.DataField, fi.FilterValue()).ToList(); return results; } So let’s break it down a little. First, the first parameter of a lambda expression, which is what we are building here, is the input parameter, or in this case, the x portion of x =>.var param = Expression.Parameter(typeof(T), "x");Next, we’re setting up the call which is made to the right of the => of the Lambda expression. The code below is saying “grab the method name provided by the operatorName variable, which can be found in any class which implements the BusinessObjectBase class.typeof(BusinessObjectBase).GetMethod(operatorName,The result of the expression defined above will be returning a boolean, as defined here:var lambda = Expression.Lambda<Func<T, bool>>(body, param);Finally, we return the data which evaluates in the lambda expression as true via this line of code:return data.Where(lambda.Compile());Note the delegate lambda expression is compiled into executrable code and produces a delegate which represents the lambda expression.Obviously, understanding how to dynamically call methods and compare properties using LINQ is extremely powerful and has many,many possibilities. I hope this helps someone else trying to do the same thing!
var body = Expression.Call( typeof(BusinessObjectBase).GetMethod(operatorName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public),
public static IEnumerable<T> RunOperator<T>(this IEnumerable<T> data, string operatorName, string propertyOrFieldName, string searchValue) { var param = Expression.Parameter(typeof(T), "x"); var body = Expression.Call( typeof(BusinessObjectBase).GetMethod(operatorName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), Expression.PropertyOrField(param, propertyOrFieldName), Expression.Constant(searchValue, typeof(string))); var lambda = Expression.Lambda<Func<T, bool>>(body, param); return data.Where(lambda.Compile()); }
static bool Like(string a, string b) { return a.ToLower().Contains(b.ToLower()); } static bool Equals(string a, string b) { return a.ToLower() == b.ToLower(); }
public List<T> FilterData<T>(List<T> data, bool wildcardSearch) { List<T> results = data; if (wildcardSearch) results = results.RunOperator("Like", fi.DataField, fi.FilterValue()).ToList(); else results = results.RunOperator("Equals", fi.DataField, fi.FilterValue()).ToList(); return results; }
var param = Expression.Parameter(typeof(T), "x");
typeof(BusinessObjectBase).GetMethod(operatorName,
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
return data.Where(lambda.Compile());
Powered by: newtelligence dasBlog 2.3.9074.18820
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, © Copyright 2010
E-mail