我有一个通用的分页方法,我试图调用。 但我得到一个编译时错误: 方法的类型参数不能从使用中推断出来
方法:
public static IQueryable<T> OrderedPagedResults<T, TResult, TType>(IQueryable<T> query, int pageNum, int pageSize, Expression<Func<T, TResult>> orderByProperty, bool isAscendingOrder, out int rowsCount, List<KeyValuePair<Expression<Func<T, TType>>, bool>> lstThenByConditions = null) { if (pageSize <= 0) pageSize = 20; rowsCount = query.Count(); if (rowsCount <= pageSize || pageNum <= 0) pageNum = 1; var excludedRows = (pageNum - 1) * pageSize; query = isAscendingOrder ? query.OrderBy(orderByProperty) : query.OrderByDescending(orderByProperty); if (lstThenByConditions != null && lstThenByConditions.Any()) { foreach (var thenByProperty in lstThenByConditions) { if (!thenByProperty.Equals(default(KeyValuePair<Expression<Func<T, TType>>, bool>)) && (typeof(IOrderedQueryable<T>).IsAssignableFrom(query.Expression.Type))) { query = thenByProperty.Value ? (query as IOrderedQueryable<T>).ThenBy(orderByProperty) : (query as IOrderedQueryable<T>).ThenByDescending(orderByProperty); } } } return query.Skip(excludedRows).Take(pageSize); }我试图将其调用为:
var resultset = OrderedPagedResults(employees, pageNum, rowNum, o => o.JoiningDate, isSortAscending, out totalRows);employees = IQueryable由于某种原因,我收到此编译时错误,我无法调用它。
有什么建议我在这里缺少什么?
I have a generic method for paging which I am trying to invoke. But I am getting a compile time error: The type arguments for method cannot be inferred from usage
Method:
public static IQueryable<T> OrderedPagedResults<T, TResult, TType>(IQueryable<T> query, int pageNum, int pageSize, Expression<Func<T, TResult>> orderByProperty, bool isAscendingOrder, out int rowsCount, List<KeyValuePair<Expression<Func<T, TType>>, bool>> lstThenByConditions = null) { if (pageSize <= 0) pageSize = 20; rowsCount = query.Count(); if (rowsCount <= pageSize || pageNum <= 0) pageNum = 1; var excludedRows = (pageNum - 1) * pageSize; query = isAscendingOrder ? query.OrderBy(orderByProperty) : query.OrderByDescending(orderByProperty); if (lstThenByConditions != null && lstThenByConditions.Any()) { foreach (var thenByProperty in lstThenByConditions) { if (!thenByProperty.Equals(default(KeyValuePair<Expression<Func<T, TType>>, bool>)) && (typeof(IOrderedQueryable<T>).IsAssignableFrom(query.Expression.Type))) { query = thenByProperty.Value ? (query as IOrderedQueryable<T>).ThenBy(orderByProperty) : (query as IOrderedQueryable<T>).ThenByDescending(orderByProperty); } } } return query.Skip(excludedRows).Take(pageSize); }I am trying to invoke this as:
var resultset = OrderedPagedResults(employees, pageNum, rowNum, o => o.JoiningDate, isSortAscending, out totalRows);where employees = IQueryable Due to some reason I am getting this compile time error and I am not able to invoke this.
Any suggestion what I am missing here?
最满意答案
虽然相当聪明,编译器并不总是能够推断泛型类型,例如这里的情况,因为有多种泛型类型。 它是如何知道它们应该是什么的? 你需要更明确:
var resultset = OrderedPagedResults<IEnumerable<Employee>, int, int> (employees, pageNum, rowNum, o => o.JoiningDate, isSortAscending, out totalRows);(我猜那里的类型)
Eric Lippert对此有一个很好的解释,但我不记得它在哪里。
The compiler, whilst fairly smart, isn't always able to infer generic types, such as the case here, because there are multiple generic types. How does it know what they are supposed to be? You need to be more explicit:
var resultset = OrderedPagedResults<IEnumerable<Employee>, int, int> (employees, pageNum, rowNum, o => o.JoiningDate, isSortAscending, out totalRows);(I'm guessing at the types there)
Eric Lippert has a great explanation of this somewhere, but I can't recall where it is.
更多推荐
发布评论