How is it possible to filter data based on some set of filters or expressions that should be used with
For example, there is a class:
It is required to filter
or
operation in where clause? For example, there is a class:
class DTOFilter
{
public string Domain { get; set; }
public string Mobile { get; set; }
}
It is required to filter
Users
list based on the list of the filters next way:u=>
(u.Email.Contains(filters[0].Domain)
&& u.PhoneNumber.StartsWith(filters[0].Mobile)) ||
(u.Email.Contains(filters[1].Domain)
&& u.PhoneNumber.StartsWith(filters[1].Mobile)) ||
...
But the more usable form would be:db.Users.Where(filters.Filter<Users, DTOFilter>(
(u, f) => u.Email.Contains(f.Domain)
&& u.PhoneNumber.StartsWith(f.Mobile))
.Or())
To perform that task it's required to implement two functions:
Filter split data function, should multiply the selector expression and replace in the result expression parameter with the exact object value. Filter data population is done in the function:
To replace parameter with the exact value was implemented simple visitor:
Or
expression join function- Filter data split function
Or
expression like (((expression1 or expression2) or expression3) or expression4)
public static Expression<Func<T, bool>> Or<T>(
this IEnumerable<Expression<Func<T, bool>>> source)
{
var expressions = source.ToList();
if (expressions.Count == 0)
return x => false;
var orExpression = expressions
.Select(e => e.Body)
.Aggregate((a, b) => Expression.OrElse(a, b));
var parameter = expressions.First().Parameters.First();
return Expression.Lambda<Func<T, bool>>(orExpression, parameter);
}
This function can already be used if filters are already valid expressions.Filter split data function, should multiply the selector expression and replace in the result expression parameter with the exact object value. Filter data population is done in the function:
public static IEnumerable<Expression<Func<T, bool>>> Filter<T, TData>(
this IEnumerable<TData> data,
Expression<Func<T,TData, bool>> selector)
{
var parameter = selector.Parameters.First(
p => p.Type.IsAssignableFrom(typeof(T)));
return data.Select(item => Expression.Lambda<Func<T, bool>>(
new DataVisitor<TData>(item).Visit(selector.Body), parameter));
}
To replace parameter with the exact value was implemented simple visitor:
public class DataVisitor<T> : ExpressionVisitor
{
private readonly ConstantExpression _data;
public DataVisitor(T dataItem)
{
_data = Expression.Constant(dataItem);
}
protected override Expression VisitParameter(
ParameterExpression node)
{
return node.Type.IsAssignableFrom(typeof(T))
? _data : base.VisitParameter(node);
}
}
The build expression is a correct lambda expression that can be parsed by the EF expression tree parser. (Check community comments)
Comments
Post a Comment