Create expression <> for filtering by custom property
I want to write filter controls that take the object type T
and property name and return Expression<Func<T, bool>>
that checks the value of the passed property. I don't want to use reflection because I am afraid that expressions like this cannot be used by EF. I cannot use delegates because C # has no delegates for properties. What can I do? Maybe I should use a different approach for writing these controls?
Here's my first approach using reflection:
public string FilteringField { get; set; } public Expression<Func<T, bool>> GetFilterExpression() { if (cmbValue.SelectedIndex == 1) return (o => (bool)typeof(T).GetProperty(FilteringField).GetValue(o, null)); if (cmbValue.SelectedIndex == 2) return (o => !(bool)typeof(T).GetProperty(FilteringField).GetValue(o, null)); return null; }
source share
Reflection is not a problem here; EF won't even be able to tell the difference. By the way, the delegate approach is not a starter (since you mention EF); ultimately, it is something like:
public static IQueryable<T> Where<T>(this IQueryable<T> query, string propertyName, object value) { PropertyInfo prop = typeof(T).GetProperty(propertyName); var param = Expression.Parameter(typeof(T), "x"); var body = Expression.Equal( Expression.Property(param, prop), Expression.Constant(value, prop.PropertyType) ); var predicate = Expression.Lambda<Func<T, bool>>(body, param); return query.Where(predicate); }
Note that with help Expression.PropertyOrField(propertyName)
you can make this easier; the reason i didn't use is that it's very convenient to know the member type ( prop.PropertyType
) here when creating a constant - otherwise you might get problems with nulls.
source share
I know this is an old answer, but in case anyone sees this, I have built this project:
https://github.com/PoweredSoft/DynamicLinq
Which should be downloaded and on nuget:
https://www.nuget.org/packages/PoweredSoft.DynamicLinq
and you could just do
query.Where("FirstName", ConditionOperators.Equal, "David");
source share