我想创建动态lambda表达式,以便我可以使用一组过滤参数过滤列表。这就是我到目前为止所做的:
表达式是使用下面的方法构建的,其中T是列表的对象类型
public static Expression<Func<T, bool>> GetExpression<T>(IList<DynamicFilter> filters)
{
if (filters.Count == 0)
return null;
ParameterExpression param = Expression.Parameter(typeof(T), "t");
Expression exp = null;
if (filters.Count == 1)
exp = GetExpression<T>(param, filters[0]);
[...]
return Expression.Lambda<Func<T, bool>>(exp, param);
}
private static Expression GetExpression<T>(ParameterExpression param, DynamicFilter filter)
{
MemberExpression member = Expression.Property(param, filter.PropertyName);
ConstantExpression constant = Expression.Constant(filter.Value);
[...]
return Expression.Call(member, filterMethod, constant);
}
然后我打电话给
List<Example> list = ...;
var deleg = ExpressionBuilder.GetExpression<Example>(dynFiltersList).Compile();
list = list.Where(deleg).ToList();
这与一个只包含简单类型的对象一样正常,但如果内部有复杂类型,则代码不再起作用。例如,假设我在Example类中有自定义类型Field的成员,而Field有一个字符串属性Value。如果filter.PropertyName
为'Field0'(类型为Field),则代码可以正常工作,但如果我有'Field0.Value',我会得到一个明显的错误,指出没有名为'Field0.Value的属性'内部示例。
我尝试修改表达式构建方法,如下所示:
MemberExpression member = null;
if (filter.PropertyName.Contains('.'))
{
string[] props = filter.PropertyName.Split('.');
ParameterExpression param1 = Expression.Parameter(typeof(T).GetProperty(props[0]).PropertyType, "t1");
member = Expression.Property(param1, props[0]);
}
else
{
member = Expression.Property(param, filter.PropertyName);
}
但是编译表达式时出现Lambda parameter not in scope
错误。我理解为什么会出现这个错误,但我不知道如何使这个工作。
底线是我需要在形成MemberExpression时使表达式构建方法递归地工作。我最终需要获得一个list = list.Where(deleg).ToList();
,转换为类似list = list.Where(obj => obj.Field0.Value == 'something').ToList();
我刚开始使用表达式,所以我在这方面并不太了解,但任何帮助都会受到赞赏。
由于
答案 0 :(得分:2)
我意识到这是一个相当古老的帖子,但我遇到了完全相同的问题,并在Mark Gravell发布here的答案中找到了一些接近的答案。我只是稍微修改它以满足我的需要,结果如下:
private Expression GetDeepProperty(Expression parameter, string property)
{
var props = property.Split('.');
var type = parameter.Type;
var expr = parameter;
foreach (var prop in props)
{
var pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
return expr;
}
实现:
var method = typeof (string).GetMethod("Contains", new Type[] {typeof (string)}, null);
var lambdaParameter = Expression.Parameter(typeof(TEntity), "te");
var filterExpression = Expression.Lambda<Func<TEntity, bool>> (
filters.Select(filter => Expression.Call(GetDeepProperty(lambdaParameter, filter.Property),
method,
Expression.Constant(filter.Value))).
Where(exp => exp != null).
Cast<Expression>().
ToList().
Aggregate(Expression.Or), lambdaParameter);
答案 1 :(得分:1)
按照此处的说明查看ExpressionVisitor:Replacing the parameter name in the Body of an Expression
答案 2 :(得分:1)
我正在努力解决
这样我就可以使用一组过滤参数来过滤列表
不是使用ExpressionBuilder
,而是使用通用的Filter类。
public class Filter<T> where T: class
{
private readonly Predicate<T> criteria;
public Filter(Predicate<T> criteria)
{
this.criteria = criteria;
}
public bool IsSatisfied(T obj)
{
return criteria(obj);
}
}
首先我们需要一些课程。
public class Player
{
public string Name { get; set; }
public int Level { get; set; }
public enum Sex { Male, Female, Other };
public Weapon Weapon { get; set; }
}
public class Weapon
{
public string Name { get; set; }
public int MaxDamage { get; set; }
public int Range { get; set; }
public WeaponClass Class { get; set; }
public enum WeaponClass { Sword, Club, Bow }
}
然后我们需要一个对象列表。
var graywand = new Weapon { Name = "Graywand", MaxDamage = 42, Range = 1, Class = Weapon.WeaponClass.Sword };
var scalpel = new Weapon { Name = "Scalpel", MaxDamage = 33, Range = 1, Class = Weapon.WeaponClass.Sword };
var players = new List<Player> {
new Player { Name = "Fafhrd", Level = 19, Weapon = graywand },
new Player { Name = "Gray Mouser", Level = 19, Weapon = scalpel },
new Player { Name = "Freddy", Level = 9, Weapon = graywand },
new Player { Name = "Mouse", Level = 8, Weapon = scalpel}
};
然后让我们创建几个过滤器并将它们添加到列表中。
var powerfulSwords = new Filter<Player>(p => p.Weapon.MaxDamage>35);
var highLevels = new Filter<Player>(p => p.Level>15);
var filters = new List<Filter<Player>>();
filters.Add(powerfulSwords);
filters.Add(highLevels);
最后按这些过滤器过滤列表
var highLevelAndPowerfulSwords = players.Where(p => filters.All(filter => filter.IsSatisfied(p)));
var highLevelOrPowerfulSwords = players.Where(p => filters.Any(filter => filter.IsSatisfied(p)));
只有“Fafhrd”会在highLevelAndPowerfulSwords
中,highLevelOrPowerfulSwords
将包含所有玩家,但“鼠标”。
答案 3 :(得分:-1)
您可以为过滤器中的每个元素生成一个表达式,并使用以下方法将它们组合成一个单独的表达式:
public static Expression<Func<T, K>> CombineAnd<T, K>(Expression<Func<T, K>> a, Expression<Func<T, K>> b)
{
ParameterExpression firstParameter = a.Parameters.First();
Expression<Func<T, K>> b1 = Expression.Lambda<Func<T, K>>(Expression.Invoke(b, firstParameter), firstParameter);
return Expression.Lambda<Func<T, K>>(Expression.And(a.Body, b1.Body), firstParameter);
}
public static Expression<Func<T, K>> CombineOr<T, K>(Expression<Func<T, K>> a, Expression<Func<T, K>> b)
{
ParameterExpression firstParameter = a.Parameters.First();
Expression<Func<T, K>> b1 = Expression.Lambda<Func<T, K>>(Expression.Invoke(b, firstParameter), firstParameter);
return Expression.Lambda<Func<T, K>>(Expression.Or(a.Body, b1.Body), firstParameter);
}