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);
}

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.