'ASP.NET Core Web API - How to perform dynamic searching in server-side pagination

In ASP.NET Core-5 Web API, I have this code in server-side pagination.

Helper:

PagedList

public class PagedList<T> : List<T>
{
    public int CurrentPage { get; private set; }
    public int TotalPages { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }

    public bool HasPrevious => CurrentPage > 1;
    public bool HasNext => CurrentPage < TotalPages;

    public PagedList(List<T> items, int count, int pageNumber, int pageSize)
    {
        TotalCount = count;
        PageSize = pageSize;
        CurrentPage = pageNumber;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);

        AddRange(items);
    }

    public static PagedList<T> ToPagedList(IQueryable<T> source, int pageNumber, int pageSize)
    {
        var count = source.Count();
        var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();

        return new PagedList<T>(items, count, pageNumber, pageSize);
    }
}

QueryStringParameters

public abstract class QueryStringParameters
{
    const int maxPageSize = 50;
    public int PageNumber { get; set; } = 1;

    private int _pageSize = 10;
    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            _pageSize = (value > maxPageSize) ? maxPageSize : value;
        }
    }
}

public class EmployeeParameters : QueryStringParameters
{
    public uint MinYearOfBirth { get; set; }
    public uint MaxYearOfBirth { get; set; } = (uint)DateTime.Now.Year;

    public bool ValidYearRange => MaxYearOfBirth > MinYearOfBirth;

    public string FirstName { get; set; }
    public string LastName { get; set; }        
    public string Gender { get; set; }
    public int DepartmentId { get; set; }
}

Then I have this model:

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int DepartmentId { get; set; }
    public int RegNo { get; set; }
    public string Gender { get; set; }
    public DateTime DateOfBirth { get; set; }

    [ForeignKey("DepartmentId")]
    public virtual Department Department { get; set; }
}

public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Employee> Employees { get; set; }
}

Then I applied it here:

public PagedList<Employee> GetEmployees(EmployeeParameters employeeParameters)
{
    var employees = FindByCondition(o => o.DateOfBirth.Year >= employeeParameters.MinYearOfBirth &&
                                o.DateOfBirth.Year <= employeeParameters.MaxYearOfBirth);

    SearchByName(ref employees, employeeParameters.FirstName);

    return PagedList<Employee>.ToPagedList(employees.OrderBy(on => on.FirstName),
        employeeParameters.PageNumber,
        employeeParameters.PageSize);
}

private void SearchByName(ref IQueryable<Employee> employees, string firstName)
{
    if (!employees.Any() || string.IsNullOrWhiteSpace(employeeName))
        return;

    employees = employees.Where(o => o.FirstName.ToLower().Contains(employeeName.Trim().ToLower()));
}

What I have in my code above will only search by the FirstName of the Employee.

How do I search and sort by FirstName, LastName, Gender, DateOfBirth and Name of Department?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source