// get employee list filtered public async Task <LisitngModels.ListingResult> GetListFiltered(LisitngModels.ListingRequest request) { // filters var filters = new List <Expression <Func <Employee, bool> > >(); // employee name filter if (request.EmpId.HasValue) { filters.Add(x => x.Id == request.EmpId); } // state filter if (!string.IsNullOrEmpty(request.State)) { filters.Add(x => x.State == request.State); } // pay posting date start if (request.PayPostingFrom.HasValue) { filters.Add(x => x.Payments.Any(y => y.CreateDateTime >= request.PayPostingFrom)); } // pay posting date end if (request.PayPostingTo.HasValue) { filters.Add(x => x.Payments.Any(y => y.CreateDateTime < request.PayPostingTo.Value.AddDays(1))); } // get the filtered list List <Employee> filteredList; using (var db = new AppDbContext()) { // get all employees with their payments var employees = db.Employees.Include(x => x.Payments).OrderByDescending(x => x.Id).AsQueryable(); // apply filters if (filters.Any()) { foreach (var filter in filters) { employees = employees.Where(filter); } } // get filtered list filteredList = await employees.ToListAsync(); } // free form search if (!string.IsNullOrEmpty(request.SearchPhrase)) { // get space seperated words var words = request.SearchPhrase.Split(' '); foreach (var word in words) { var lowerword = word.ToLower(); filteredList = filteredList.Where(x => // search applicaple fields x.FirstName.ToLower().Contains(lowerword) || x.LastName.ToLower().Contains(lowerword) || x.State.ToLower().Contains(lowerword) || x.Payments.Any(y => y.GrossPay.ToString().Equals(lowerword)) || x.Payments.Any(y => y.NetPay.ToString().Equals(lowerword)) ).ToList(); } } // get transformed list var transformedList = filteredList.Select(TransformListingItem).ToList(); // sort the list var sortedList = GetSortedList(transformedList, request.Sort); // get parial list var partialList = GetPartialList(request.Current, request.RowCount, sortedList); // return list return(new LisitngModels.ListingResult { Current = request.Current, RowCount = request.RowCount, Total = filteredList.Count, Rows = partialList }); }
public async Task <IActionResult> GetListFiltered(LisitngModels.ListingRequest request) { // return filtered list return(Ok(await ListingService.GetListFiltered(request))); }