因此,我尝试构建一个从IQueryable获取Datetime属性的通用表达式,并对其应用Day比较。但是,我一直收到错误的错误提供的参数数量。
我的功能如下:
public IQueryable<T> SetDateCompare<T>(IQueryable<T> OriginalQuery, Expression<Func<T, DateTime>> getDateFunc, DateTime ComparisonDate, bool isGreaterThan = true)
where T : class
{
if (isGreaterThan)
{
Expression left = Expression.Call(getDateFunc.Body, typeof(DateTime).GetMethod("get_Day"));
Expression right = Expression.Constant(ComparisonDate.Day, typeof(int));
Expression res = Expression.GreaterThan(left, right);
//var whereCall = Expression.Lambda<Func<T,bool>>(Expression.GreaterThanOrEqual(left, right), ).
MethodCallExpression whereCall = Expression.Call(typeof(Queryable),
"Where",
new Type[] { OriginalQuery.ElementType },
OriginalQuery.Expression,
Expression.Lambda<Func<string, bool>>(res), getDateFunc.Parameters.Single());
OriginalQuery.Provider.CreateQuery<T>(whereCall);
}
return OriginalQuery;
}
有谁知道我能做些什么来解决这个问题?
答案 0 :(得分:1)
尝试修复您的代码:
public IQueryable<T> SetDateCompare<T>(IQueryable<T> OriginalQuery, Expression<Func<T, DateTime>> getDateFunc, DateTime ComparisonDate, bool isGreaterThan = true)
where T : class
{
if (isGreaterThan)
{
Expression left = Expression.Call(getDateFunc.Body, typeof(DateTime).GetMethod("get_Day"));
Expression right = Expression.Constant(ComparisonDate.Day, typeof(int));
Expression res = Expression.GreaterThan(left, right);
var whereCallLambda = Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(left, right), getDateFunc.Parameters.Single());
MethodCallExpression whereCall = Expression.Call(typeof(Queryable),
"Where",
new Type[] { OriginalQuery.ElementType },
OriginalQuery.Expression,
whereCallLambda);
OriginalQuery = OriginalQuery.Provider.CreateQuery<T>(whereCall);
}
return OriginalQuery;
}