Linq 表达式构建为 lambda 声明提供的参数数量不正确
Linq Expression Building Incorrect number of parameters supplied for lambda declaration
所以我正在尝试构建一个从 IQueryable 获取日期时间 属性 的通用表达式,并对其应用 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;
}
有谁知道我可以做些什么来解决这个问题?
尝试像这样修复您的代码:
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;
}
所以我正在尝试构建一个从 IQueryable 获取日期时间 属性 的通用表达式,并对其应用 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;
}
有谁知道我可以做些什么来解决这个问题?
尝试像这样修复您的代码:
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;
}