I want to write filtering controls which take object type T
and property name and return Expression<Func<T, bool>>
that checks value of passed property. I don't want to use reflection because I'm afraid such expressions can't be used by EF. I can't use delegates because C# doesn't have delegates for properties. What can I do? Maybe I should use different approach for writing these controls?
Here is 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;
}