Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 789
Steven Avatar asked Jan 28 '11 01:01

Steven


1 Answers

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

like image 109
James Joyce Avatar answered Oct 31 '22 13:10

James Joyce