Maninder
Maninder

Reputation: 1261

The type arguments for method cannot be inferred from usage error

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?

Upvotes: 0

Views: 3918

Answers (2)

Eric Lippert
Eric Lippert

Reputation: 660138

Stuart Grassie's answer is correct but omits the detail which explains why you are getting the error. In your example, from which argument is the compiler supposed to deduce the type argument you intended for type parameter TType?

I can't find any such argument; the compiler cannot either, and therefore you have not given it enough information from which to infer the type.

Upvotes: 3

Stuart Grassie
Stuart Grassie

Reputation: 3073

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.

Upvotes: 3

Related Questions