public IMemberGroup GetById(int id) { using (var scope = ScopeProvider.CreateScope(autoComplete: true)) { return(_memberGroupRepository.Get(id)); } }
public IEnumerable <IMember> FindMembersInRole(string roleName, string usernameToMatch, StringPropertyMatchType matchType = StringPropertyMatchType.StartsWith) { //get the group id var grpQry = Query <IMemberGroup>().Where(group => group.Name.Equals(roleName)); var memberGroup = _memberGroupRepository.Get(grpQry).FirstOrDefault(); if (memberGroup == null) { return(Enumerable.Empty <IMember>()); } // get the members by username var query = Query <IMember>(); switch (matchType) { case StringPropertyMatchType.Exact: query.Where(member => member.Username.Equals(usernameToMatch)); break; case StringPropertyMatchType.Contains: query.Where(member => member.Username.Contains(usernameToMatch)); break; case StringPropertyMatchType.StartsWith: query.Where(member => member.Username.StartsWith(usernameToMatch)); break; case StringPropertyMatchType.EndsWith: query.Where(member => member.Username.EndsWith(usernameToMatch)); break; case StringPropertyMatchType.Wildcard: query.Where(member => member.Username.SqlWildcard(usernameToMatch, TextColumnType.NVarchar)); break; default: throw new ArgumentOutOfRangeException(nameof(matchType)); } var matchedMembers = Get(query).ToArray(); var membersInGroup = new List <IMember>(); //then we need to filter the matched members that are in the role //since the max sql params are 2100 on sql server, we'll reduce that to be safe for potentially other servers and run the queries in batches var inGroups = matchedMembers.InGroupsOf(1000); foreach (var batch in inGroups) { var memberIdBatch = batch.Select(x => x.Id); var sql = Sql().SelectAll().From <Member2MemberGroupDto>() .Where <Member2MemberGroupDto>(dto => dto.MemberGroup == memberGroup.Id) .Where("Member IN (@memberIds)", new { memberIds = memberIdBatch }); var memberIdsInGroup = Database.Fetch <Member2MemberGroupDto>(sql) .Select(x => x.Member).ToArray(); membersInGroup.AddRange(matchedMembers.Where(x => memberIdsInGroup.Contains(x.Id))); } return(membersInGroup); }
public IEnumerable <IMember> FindMembersInRole(string roleName, string usernameToMatch, StringPropertyMatchType matchType = StringPropertyMatchType.StartsWith) { //get the group id var grpQry = Query <IMemberGroup>().Where(group => group.Name.Equals(roleName)); var memberGroup = _memberGroupRepository.Get(grpQry).FirstOrDefault(); if (memberGroup == null) { return(Enumerable.Empty <IMember>()); } // get the members by username var query = Query <IMember>(); switch (matchType) { case StringPropertyMatchType.Exact: query.Where(member => member.Username.Equals(usernameToMatch)); break; case StringPropertyMatchType.Contains: query.Where(member => member.Username.Contains(usernameToMatch)); break; case StringPropertyMatchType.StartsWith: query.Where(member => member.Username.StartsWith(usernameToMatch)); break; case StringPropertyMatchType.EndsWith: query.Where(member => member.Username.EndsWith(usernameToMatch)); break; case StringPropertyMatchType.Wildcard: query.Where(member => member.Username.SqlWildcard(usernameToMatch, TextColumnType.NVarchar)); break; default: throw new ArgumentOutOfRangeException(nameof(matchType)); } var matchedMembers = Get(query).ToArray(); var membersInGroup = new List <IMember>(); //then we need to filter the matched members that are in the role foreach (var group in matchedMembers.Select(x => x.Id).InGroupsOf(Constants.Sql.MaxParameterCount)) { var sql = Sql().SelectAll().From <Member2MemberGroupDto>() .Where <Member2MemberGroupDto>(dto => dto.MemberGroup == memberGroup.Id) .WhereIn <Member2MemberGroupDto>(dto => dto.Member, group); var memberIdsInGroup = Database.Fetch <Member2MemberGroupDto>(sql) .Select(x => x.Member).ToArray(); membersInGroup.AddRange(matchedMembers.Where(x => memberIdsInGroup.Contains(x.Id))); } return(membersInGroup); }