2
\$\begingroup\$

I'm using X.PagedList for paging functionality in my application. The code I have works fine but when I return larger sets of data it seems like it could be faster. Is there anything I can do to my code to have it perform more efficiently?

Controller Code

var employees = _service.GetEmployeesByOffice(region, area, office).OrderBy(e => e.FullName);
var model = Mapper.Map<IEnumerable<Employee>,IEnumerable<EmployeesViewModel>>(employees).ToPagedList(page, 20);

var viewModel = new EmployeeListingView
{
    EmployeeViewModel = model,
    // Additional...
};

return View(viewModel);

Service Call

public IEnumerable<Employee> GetEmployeesByOffice(string region, string area, string office)
{
    var query = _context.Employees.AsQueryable();

    if (!String.IsNullOrEmpty(region))
        query = query.Where(e => e.Region == region);

    if (!String.IsNullOrEmpty(area))
        query = query.Where(e => e.Area == area);

    if (!String.IsNullOrEmpty(office))
        query = query.Where(e => e.Office == office);

    return query;
}

Razor Page

@Html.PagedListPager((IPagedList)Model.EmployeeViewModel, page => Url.Action("Index", new { area = Model.SelectedArea, office = Model.SelectedOffice, page }))
\$\endgroup\$
1
\$\begingroup\$

I cannot say anything about the mapping but you can simplify this query

public IEnumerable<Employee> GetEmployeesByOffice(string region, string area, string office)
{
    var query = _context.Employees.AsQueryable();

    if (!String.IsNullOrEmpty(region))
        query = query.Where(e => e.Region == region);

    if (!String.IsNullOrEmpty(area))
        query = query.Where(e => e.Area == area);

    if (!String.IsNullOrEmpty(office))
        query = query.Where(e => e.Office == office);

    return query;
}

into this one. Each expression will resove either to true or the actual condition if the variable was provided.

public IEnumerable<Employee> GetEmployeesByOffice(string region, string area, string office)
{
    return
        from e in _context.Employees.AsQueryable()
        where
            (string.IsNullOrEmpty(region) || e.Region == region) &&
            (string.IsNullOrEmpty(area) || e.Area == area) &&
            (string.IsNullOrEmpty(office) || e.Office == office);
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for the tip! I figured there was a way to simplify this but wasn't sure. \$\endgroup\$ – DrivenTooFar Apr 19 '17 at 13:44
1
\$\begingroup\$

So it seems that the issue was with the mapping. Using this answer, I created an extension to handle mapping the object after calling it. I changed this code

var employees = 
      _service
          .GetEmployeesByOffice(region, area, office)
          .OrderBy(e => e.FullName);
var model = 
      Mapper.Map<IEnumerable<Employee>, IEnumerable<EmployeesViewModel>>(employees)
      .ToPagedList(page, 20);

to this

var model = 
    _service
        .GetEmployeesByOffice(region, sArea, sOffice)
        .OrderBy(e => e.FullName)
        .ToPagedList(page, 20)
        .ToMappedPagedList<Employee, EmployeesViewModel>();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.