public async Task <CustomerSearchResponseDTO> SearchCustomerAsync(CustomerSearchRequestDTO request) { var projects = this.insuranceContext.Customers .AsQueryable(); if (!string.IsNullOrEmpty(request.Name)) { projects = projects.Where(p => EF.Functions.Like(p.FullName, request.Name.AddwhildChars())); } var projectSerachQuery = projects.Select(CustomerProjections.CustomerProjection); if (!string.IsNullOrEmpty(request.Pagination.SortBy)) { if (request.Pagination.SortBy == "FirstName") { projectSerachQuery = request.Pagination.SortOrder == "desc" ? projectSerachQuery.OrderByDescending(it => it.FirstName) : projectSerachQuery.OrderBy(it => it.FirstName); } if (request.Pagination.SortBy == "CustomerId") { projectSerachQuery = request.Pagination.SortOrder == "desc" ? projectSerachQuery.OrderByDescending(it => it.CustomerId) : projectSerachQuery.OrderBy(it => it.CustomerId); } } else { //default order by Name projectSerachQuery = request.Pagination.SortOrder == "desc" ? projectSerachQuery.OrderByDescending(it => it.LastName) : projectSerachQuery.OrderBy(it => it.LastName); } var pagedResults = await PaginatedList <CustomerDto> .CreateAsync(projectSerachQuery.AsNoTracking(), request.Pagination.PageIndex, request.Pagination.PageSize); CustomerSearchResponseDTO response = new CustomerSearchResponseDTO() { SearchResponseData = pagedResults.ToList(), Pagination = PaginatedList <CustomerDto> .ConvertToPagination(pagedResults) }; return(response); }