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

查看:687
本文介绍了无法从用法错误中推断出方法的类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我试图调用的通用分页方法. 但是我遇到了编译时错误: 无法从用法中推断出方法的类型参数

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

方法:

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);

其中员工= IQueryable 由于某种原因,我遇到了此编译时错误,因此无法调用它.

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?

推荐答案

尽管编译器相当聪明,但由于存在多个通用类型,因此并不总是能够推断通用类型,例如此处的情况.它怎么知道他们应该是什么?您需要更加明确:

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);

(我猜那里的类型)

埃里克·利珀特(Eric Lippert)在某处对此有很好的解释,但我不记得它在哪里.

Eric Lippert has a great explanation of this somewhere, but I can't recall where it is.

这篇关于无法从用法错误中推断出方法的类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆