public IEnumerable <Person> Search(SearchPersonCriteria criteria) { if (criteria == null) { throw new ArgumentNullException(nameof(criteria)); } if (ShouldUseExactName(criteria) && ShouldUsePartialName(criteria)) { throw new ArgumentException( "Cannot use partial and exact search at the same time.", nameof(criteria)); } var query = this.dbContext.Persons.AsQueryable(); if (ShouldUseExactName(criteria)) { query = query.Where(p => p.Name == criteria.ExactName); } if (ShouldUsePartialName(criteria)) { query = query.Where(p => p.Name.Contains(criteria.PartialName)); } if (criteria.GroupId.HasValue) { query = query.Where(p => p.GroupId == criteria.GroupId.Value); } return(query.ToArray()); }
private static bool ShouldUsePartialName(SearchPersonCriteria criteria) { return(!string.IsNullOrEmpty(criteria.PartialName)); }