/// <summary> /// Gets the communication list members. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="listGroupId">The list group identifier.</param> /// <param name="segmentCriteria">The segment criteria.</param> /// <param name="segmentDataViewIds">The segment data view ids.</param> /// <returns></returns> public static IQueryable <GroupMember> GetCommunicationListMembers(RockContext rockContext, int?listGroupId, SegmentCriteria segmentCriteria, List <int> segmentDataViewIds) { IQueryable <GroupMember> groupMemberQuery = null; if (listGroupId.HasValue) { var groupMemberService = new GroupMemberService(rockContext); var personService = new PersonService(rockContext); var dataViewService = new DataViewService(rockContext); groupMemberQuery = groupMemberService.Queryable().Where(a => a.GroupId == listGroupId.Value && a.GroupMemberStatus == GroupMemberStatus.Active); Expression segmentExpression = null; ParameterExpression paramExpression = personService.ParameterExpression; var segmentDataViewList = dataViewService.GetByIds(segmentDataViewIds).AsNoTracking().ToList(); foreach (var segmentDataView in segmentDataViewList) { List <string> errorMessages; var exp = segmentDataView.GetExpression(personService, paramExpression, out errorMessages); if (exp != null) { if (segmentExpression == null) { segmentExpression = exp; } else { if (segmentCriteria == SegmentCriteria.All) { segmentExpression = Expression.AndAlso(segmentExpression, exp); } else { segmentExpression = Expression.OrElse(segmentExpression, exp); } } } } if (segmentExpression != null) { var personQry = personService.Get(paramExpression, segmentExpression); groupMemberQuery = groupMemberQuery.Where(a => personQry.Any(p => p.Id == a.PersonId)); } } return(groupMemberQuery); }