我有一个查询,我需要对其应用n个过滤器。但是我必须使用表达式。到目前为止,我已经为每个过滤器构造了表达式,效果很好。问题是我想将这些过滤器合并为一个表达式,以便可以将其用作LINQ的.Where()的参数。
过滤器代码:
//Filters could slightly differ in functionality
private Expression<Func<T, bool>> StringPropertyContains<T>(Filter filter)
{
if (filter == null)
{
throw new ArgumentNullException(nameof(filter));
}
if (typeof(T).GetProperties().FirstOrDefault(pi => pi.Name.Equals(filter.PropertyName, StringComparison.OrdinalIgnoreCase)) == null)
{
throw new ArgumentNullException($"Type {typeof(T)} does not contain property {filter.PropertyName}");
}
var propertyInfo = typeof(T).GetProperty(filter.PropertyName);
var param = Expression.Parameter(typeof(T));
var member = Expression.MakeMemberAccess(param, propertyInfo);
var constant = Expression.Constant(filter.Value, typeof(filter.Type));
var methodInfo = typeof(filter.Type).GetMethod(filter.Method, new Type[] { typeof(filter.Type) });
var body = Expression.Call(member, methodInfo, constant);
return Expression.Lambda<Func<T, bool>>(body, param);
}
private Expression<Func<T, bool>> Filter<T>(T t)
{
//TODO join filters while each filter should have its different parameter
//the parameter is currently constructed in the object and is accessible via property Filter[] ParsedFilter
}
过滤器类别:
internal class Filter
{
public string Type { get; }
public string Method { get; }
public string PropertyName { get; }
public string Value { get; }
}
所需用法:
IQueryable<T> q = query.Where(this.Filter)
老实说,我对此问题感到非常头疼,因为我对表达式还很陌生。因此,感谢您的任何帮助。
您不能将类作为参数传递给Where子句,但是可以使用Linq Expressions来实现所需的功能:
Expression<Func<T, bool>> filter = q => q.type == filter.Type && q.Value == filter.Value;
var x = query.Where(filter);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句