9
votes

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.

6
Just add using System.Linq.Dynamic; or using System.Linq.Dynamic.Core; for core projects. - AmiNadimi

6 Answers

29
votes

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.

19
votes

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.

7
votes

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.

7
votes

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......

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

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;