我制作了通用功能,它在运行时从 JSON 过滤器 编译为 Expression 并且在我的 IQueryable 上进行 Skip 和 Take 调用时出现问题,以便完整编译(没有 exec sp_executesql)在 SQL 中
我的方法:
public static class PagingExtensions
{
public static IQueryable<T> Page<T>(this IQueryable<T> query, QueryRequest queryRequest) where T : class
{
//if (queryRequest.Skip.HasValue)
//{
//query = query.Skip(queryRequest.Skip.Value);
//}
//if (queryRequest.Take.HasValue)
//{
//query = query.Take(queryRequest.Take.Value);
//}
var methodName = "Skip";
var resultExp = Expression.Call(
typeof(IQueryable),
"Skip",
Type.EmptyTypes,
Expression.Constant(queryRequest.Skip.Value));
query = query.Provider.CreateQuery<T>(resultExp);
return query;
}
}
异常(exception):
InvalidOperationException: No method 'Skip' on type 'System.Linq.IQueryable' is compatible with the supplied arguments.
最佳答案
Skip
方法不是IQueryable
的实例方法,而是静态Queryable
类的扩展。
您需要获取通用方法信息并将源查询作为第一个参数传递:
MethodInfo method = typeof(Queryable).GetMethod("Skip").MakeGenericMethod(typeof(T));
var resultExp = Expression.Call(
method,
query.Expression,
Expression.Constant(queryRequest.Skip.Value));
关于c# - Expression.Call 跳过或接听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55257967/