Beispiel #1
0
        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());
        }
Beispiel #2
0
 private static bool ShouldUsePartialName(SearchPersonCriteria criteria)
 {
     return(!string.IsNullOrEmpty(criteria.PartialName));
 }