Steven
Steven

Reputation: 18869

The type arguments for method System.Linq.Enumerable.OrderBy cannot be inferred from the usage

I'm trying to follow the demo from this link to add a jqGrid to an MVC app.

I have a table named Companies that I'm trying to display in a grid. A Company simply contains an ID and a Name.

I'm running into an error in my controller function:

public JsonResult DynamicGridData(string sortIndex, string sortOrder, int page, int rows)
{
    int pageIndex = Convert.ToInt32(page) - 1;
    int pageSize = rows;

    var companies = companiesRepository.Companies.OrderBy(sortIndex + " " + sortOrder).Skip(pageIndex * pageSize).Take(pageSize);
    //Error here

    ...
}

I'm getting an error on the line that is calling OrderBy():

The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

I really have no idea what the error means, and I haven't been able to find an explanation. I'm not sure what is causing this error on a simple OrderBy function.

Upvotes: 14

Views: 43860

Answers (7)

Ghanshyam Tarsariya
Ghanshyam Tarsariya

Reputation: 21

For .Net Core project, need to add a reference of System.Linq.Dynamic.Core;

Upvotes: 0

drchanix
drchanix

Reputation: 51

If you are referencing System.Linq.Dynamic, don't forget to add using System.Linq.Dynamic; If you are referencing System.Linq.Dynamic.Core, don't forget to add using System.Linq.Dynamic.Core;

Upvotes: 3

Snehlata Shaw
Snehlata Shaw

Reputation: 97

  1. Add the reference System.Linq.Dynamic.dll to your project.
  2. Then, add the namespace: using System.Linq.Dynamic;

Upvotes: 2

James Joyce
James Joyce

Reputation: 1774

I know this is a school-boy error from my part, but I got this same error "CS0411 The type arguments for method 'Enumerable.OrderBy(IEnumerable, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.".

It turns out the "property" I was sorting on, was actually a function, and I had omitted the "()". It took me AGES to find!

In my code exerpt:

return _numbers.Values.OrderBy(x => x.TotalScore);

should have been

return _numbers.Values.OrderBy(x => x.TotalScore());

Just thought I would mention it......

Upvotes: 11

Edward
Edward

Reputation: 341

The specific answer to this question is you need to add

using System.Linq.Dynamic;

and you need to add a reference to Dynamic.DLL in your project.

Upvotes: 34

Amy B
Amy B

Reputation: 110161

If you consulted the documentation for the method you are calling (Enumerable.OrderBy), you would know that the parameter is a Func<TSource, TKey> and not a string.

The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

The compiler attempted to figure out how the string parameter was actually a Func and then decided that it couldn't determine what TSource is and what TKey is. It's asking you to help out by specifying those types in the call, like this:

companiesRepository.Companies.OrderBy<Company, int>(sortIndex + " " + sortOrder)

If you do that, then the compiler will instead tell you that string isn't a correct parameter, because now it has enough information to know that.

Upvotes: 8

SLaks
SLaks

Reputation: 887777

You cannot OrderBy a string; you need to pass a lambda expression or delegate.

You need to use Dynamic LINQ, as mentioned in the tutorial.

Upvotes: 22

Related Questions