我正在使用谓词构建器,如下所示:
foreach (PropertyInfo property in properties)
{
var predicate = PredicateBuilder.True<Product>();
var name = property.Name;
var value = property.GetValue(_selectedVesselData);
predicate = predicate.And(v => v.(name) == value);
var t = property.GetValue(_selectedVesselData);
}
我想使用上面的代码,但我无法在lambda表达式中检索v。(name),是否有另一种方法可以实现上述目的?
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T,bool>> expr1,Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}