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

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.


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.


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.