private static IQueryable <Employee> ApplyQueryParameters(domain.ISearchQueryEmployee searchQuery, IQueryable <Employee> query) { if (searchQuery.UnitIds.Count >= 1) { query = query.Where(e => searchQuery.UnitIds.Contains((int)e.UnitId)); } if (!string.IsNullOrEmpty(searchQuery.FirstName)) { query = query.Where(e => e.FirstName != null && e.FirstName.Contains(searchQuery.FirstName, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(searchQuery.LastName)) { query = query.Where(e => e.LastName != null && e.LastName.Contains(searchQuery.LastName, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(searchQuery.JobTitle)) { query = query.Where(e => e.JobTitle != null && e.JobTitle.Contains(searchQuery.JobTitle, StringComparison.InvariantCultureIgnoreCase)); } if (!string.IsNullOrEmpty(searchQuery.SocialSecurityNumber)) { //todo: implement search for ssn } if (searchQuery.EmployeesIds.Count > 0) { query = query.Where(e => searchQuery.EmployeesIds.Contains(e.Id)); } if (searchQuery.UserIds.Count > 0) { query = query.Where(e => e.UserId.HasValue && searchQuery.UserIds.Contains(e.UserId.Value)); } return(query); }
public async Task <IList <domain.IEmployee> > Search(domain.ISearchQueryEmployee searchQuery) { using (var context = await _dbContextFactory.CreateDbContext()) { var query = context.Employees .Where(u => !u.IsDeleted && u.EmployeeEndDate > DateTime.Today); query = ApplyQueryParameters(searchQuery, query); var employees = query .Skip(searchQuery.Skip ?? SearchConstants.DEFAULT_SKIP) .Take(searchQuery.Take ?? SearchConstants.DEFAULT_TAKE) .OrderBy(x => x.FirstName).ThenBy(x => x.LastName) .ToList(); await AttachPropertiesFromUserEntity(employees); return(employees.ToList <domain.IEmployee>()); } }