我一直在尝试为Linq创建一个表达式树过滤器,该过滤器接受2个日期和一串可能的值{“ lessthan”,“ equals”,“ morethan”}。我希望将调用的格式设置为Query.Where(CompareDates(x => x.left,right,“小于”));
我有代码:
public static IQueryable<TSource> CompareDates<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, DateTime?>> left,
DateTime? right,
string equality)
{
if (right == null || string.IsNullOrWhiteSpace(equality))
return source;
var p = left.Parameters.Single();
Expression member = p;
Expression leftExpression = Expression.Property(member, "left");
Expression rightParameter = Expression.Constant(right, typeof(DateTime));
BinaryExpression BExpression = null;
switch (equality)
{
case "lessthan":
BExpression = Expression.LessThan(leftExpression, rightParameter);
break;
case "equal":
BExpression = Expression.Equal(leftExpression, rightParameter);
break;
case "morethan":
BExpression = Expression.GreaterThan(leftExpression, rightParameter);
break;
default:
throw new Exception(String.Format("Equality {0} not recognised.", equality));
}
return source.Where(Expression.Lambda<Func<TSource, bool>>(BExpression, p));
}
不幸的是,它产生错误“ System.ArgumentException:在SARRestAPI.Extensions的System.Linq.Expressions.Expression.Property(Expression expression,String propertyName)上没有为类型'Model'定义实例属性'left'。 Expressions.CompareDates [TSource](IQueryable 1 source, Expression
1 src,提供了DateTime,字符串相等)“
任何人都知道为什么会这样吗?
答案 0 :(得分:5)
我们去了;您要执行的操作是使用传入选择器的.Body
,而不要查找.left
。意思是,给定x => x.Foo.Bar.Blap
的输入选择器,一个常量和一个比较,您想通过{em> reuse 都使用x => x.Foo.Bar.Blap < someValue
({参数(您已经在执行此操作)和正文(x
)。
在代码中(请注意,此方法适用于任何x.Foo.Bar.Blap
,而不仅仅是TValue
):
DateTime
用法示例(此处使用LINQ-to-Objects,但它也适用于其他LINQ后端):
public enum Comparison
{
Equal,
NotEqual,
LessThan,
LessThanOrEqual,
GreaterThan,
GreaterThanOrEqual
}
public static IQueryable<TSource> Compare<TSource, TValue>(
this IQueryable<TSource> source,
Expression<Func<TSource, TValue>> selector,
TValue value,
Comparison comparison)
{
Expression left = selector.Body;
Expression right = Expression.Constant(value, typeof(TValue));
BinaryExpression body;
switch (comparison)
{
case Comparison.LessThan:
body = Expression.LessThan(left, right);
break;
case Comparison.LessThanOrEqual:
body = Expression.LessThanOrEqual(left, right);
break;
case Comparison.Equal:
body = Expression.Equal(left, right);
break;
case Comparison.NotEqual:
body = Expression.NotEqual(left, right);
break;
case Comparison.GreaterThan:
body = Expression.GreaterThan(left, right);
break;
case Comparison.GreaterThanOrEqual:
body = Expression.GreaterThanOrEqual(left, right);
break;
default:
throw new ArgumentOutOfRangeException(nameof(comparison));
}
return source.Where(Expression.Lambda<Func<TSource, bool>>(body, selector.Parameters));
}
请注意,使用C#vCurrent您可以:
var arr = new[] { new { X = 11 }, new { X = 12 }, new { X = 13 }, new { X = 14 } };
var source = arr.AsQueryable();
var filtered = source.Compare(x => x.X, 12, Comparison.GreaterThan);
foreach (var item in filtered)
{
Console.WriteLine(item.X); // 13 and 14
}
您可能会更喜欢。