private IQueryable <Arrangement> ApplyFilter( IQueryable <Arrangement> arrangements, ArrangementSearchContext searchContext) { if (searchContext.Complexity.HasValue) { arrangements = arrangements.Where(e => e.Complexity >= searchContext.Complexity.Value); } if (searchContext.DateTime.HasValue) { arrangements = arrangements.Where(e => e.DateTime >= searchContext.DateTime.Value); } if (searchContext.IsHandled.HasValue) { arrangements = arrangements.Where(e => e.IsHandled == searchContext.IsHandled.Value); } if (searchContext.PersonId.HasValue) { arrangements = arrangements.Where(e => e.PersonId == searchContext.PersonId.Value); } if (searchContext.SubjectId.HasValue) { arrangements = arrangements.Where(e => e.SubjectId == searchContext.SubjectId.Value); } return(arrangements.Include(e => e.TagArrangements)); }
public async Task <List <ViewArrangementDto> > GetAsync(ArrangementSearchContext searchContext) { IEnumerable <Tag> filtrationTags; searchContext = searchContext ?? ArrangementSearchContext.Empty(); if (searchContext.Tags != null) { filtrationTags = _context.Tags.Where(e => searchContext.Tags.Contains(e.Content)).Include(e => e.TagArrangements).ToList(); } else { filtrationTags = _context.Tags.Include(e => e.TagArrangements).ToList(); } var arrangements = await ApplyFilter(_context.Arrangements.AsQueryable(), searchContext).ToListAsync(); var peopleIdDistinct = arrangements.Select(e => e.PersonId).Distinct(); var subjectsIdDistinct = arrangements.Select(e => e.SubjectId).Distinct(); var tagIds = arrangements.SelectMany(e => e.TagArrangements.Select(q => q.TagId)).Distinct(); var people = await _personService.GetAsync(e => peopleIdDistinct.Contains(e.Id)); var subjects = await _subjectService.GetAsync(e => subjectsIdDistinct.Contains(e.Id)); var tags = await _tagService.GetAsync(e => tagIds.Contains(e.Id)); return(arrangements.Select(arrangement => new ViewArrangementDto() { DateTime = arrangement.DateTime, Complexity = arrangement.Complexity, Id = arrangement.Id, IsHandled = arrangement.IsHandled, Subject = subjects.First(e => e.Id == arrangement.SubjectId).Name, SelectedPerson = people.First(e => e.Id == arrangement.PersonId).AlternativeEgo, Tags = tags.Where(t => tags.Select(e => e.Id).Intersect(arrangement.TagArrangements.Select(q => q.TagId)).Contains(t.Id)).Select(e => e.Content) }).AsQueryable().OrderBy(searchContext.OrderByCriterion).ToList()); }