I am trying to create a method where I'll pass a list of filters containing the field,comparer and value. Here is my parameter
public class QueryFilter
{
public string Field { get; set; }
public object Value { get; set; }
public string Comparer { get; set; }
}
Now, I created a method that will return an expression based on the list of QueryFilter that was passed.
public Expression<Func<TEntity, bool>> QueryFilterBuilder<TEntity>(List<QueryFilter> filters)
{
try
{
var predicate = PredicateBuilder.New<TEntity>(true);
filters.ForEach(filter =>
{
switch (filter.Comparer.ToLower())
{
case QueryFilterComparer.Eq: // equals
predicate = predicate.And(x => filter.Field == filter.value); // something like this
break;
case QueryFilterComparer.Ne: // not equal
// Add not equal filter logic here
break;
case QueryFilterComparer.Any: // any
break;
case QueryFilterComparer.Gt: // greater than
break;
case QueryFilterComparer.Gte: // greater than equal
break;
case QueryFilterComparer.Lt: // less than
break;
case QueryFilterComparer.Lte: // less than equal
break;
case QueryFilterComparer.Contains: // contains
break;
default:
break;
};
});
return predicate;
}
catch (Exception)
{
throw;
}
}
The problem is I'm not sure how to implement this with generic class. Is it possible to do this?
You can implement this using Expression api like so: