无法从使用错误中推断出方法的类型参数(The type arguments for method cannot be inferred from usage error)

编程入门 行业动态 更新时间:2024-10-13 02:21:02
无法从使用错误中推断出方法的类型参数(The type arguments for method cannot be inferred from usage error)

我有一个通用的分页方法,我试图调用。 但我得到一个编译时错误: 方法的类型参数不能从使用中推断出来

方法:

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.

更多推荐

本文发布于:2023-08-05 18:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:推断   错误   参数   类型   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!