0

When I am developing a basic Employee application of CRUD Operations using jQuery dialog and Entity Framework, I am getting two type of error when I am debugging and when I am building the solution, I know they both linked to each other but I am not able to figure out

Error 1 when building:

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

This the code(in Model class):

public IEnumerable<tblEmployee> GetEmployeePage(int pageNumber, int pageSize, string searchCriteria)
{
    if (pageNumber < 1)
        pageNumber = 1;

    return testEmp.tblEmployees
      .OrderBy(searchCriteria) //I am getting error here//
      .Skip((pageNumber - 1) * pageSize)
      .Take(pageSize)
      .ToList();
}

Error 2 when debugging:

System.ArgumentNullException: Value cannot be null.

The code as below (in View):

@model Emp_Mvc_Application.Models.PagedEmployeeModel 
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
    WebGrid grid = new WebGrid(rowsPerPage: Model.PageSize);
    grid.Bind(Model.TblEmp, autoSortAndPage: false, rowCount: Model.TotalRows);
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Raj
  • 117
  • 3
  • 4
  • 14

2 Answers2

2

Your search criteria needs to be a lambda expression e.g.

OrderBy(e => e.EmployeeID)

Instead of a string try this

public IEnumerable<tblEmployee> GetEmployeePage(int pageNumber, int pageSize, Func<tblEmployee, object> searchCriteria)
{
if (pageNumber < 1)
        pageNumber = 1;

    return testEmp.tblEmployees
      .OrderBy(searchCriteria) 
      .Skip((pageNumber - 1) * pageSize)
      .Take(pageSize)
      .ToList();
}

Call it like so:

var result = GetEmployeePage(1, 10, e => e.EmployeeId)

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
0

It looks like you are passing a string in as your orderby criteria - that method is expecting a delegate. That seems like a misleading error but I believe that is your issue.

For example:

.OrderBy(e => e.LastName)

If you need to be able to sort by a string criteria you may want to look here- How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

Community
  • 1
  • 1
Kelly Robins
  • 7,168
  • 6
  • 43
  • 66