public override async Task <bool> HandleAsync(IsUserInRoleQuery query, QueryContext context, CancellationToken cancellationToken)
        {
            IQueryable <UserRole> linq = context.DbContext.UserRoles;

            Expression <Func <UserRole, User> > selectUser = ur => ur.User;

            linq = linq.Where(selectUser.Chain(UsersHelper.GetFilterByNameWhere(query.UserName)));

            Expression <Func <UserRole, Role> > selectRole = ur => ur.Role;

            linq = linq.Where(selectRole.Chain(RolesHelper.GetFilterByNameWhere(query.RoleName)));

            return(await linq.AnyAsync(cancellationToken).ConfigureAwait(false));
        }
        public override async Task HandleAsync(RemoveUsersFromRolesCommand command, CommandContext context, CancellationToken cancellationToken)
        {
            var dbProperties = context.DbContext.GetDbProperties();

            var distinctUserNames = new HashSet <string>(dbProperties.CaseInsensitiveComparer);
            var userWhereBuilder  = PredicateBuilder <User> .False();

            foreach (var userName in command.UserNames)
            {
                if (distinctUserNames.Add(userName))
                {
                    userWhereBuilder.Or(UsersHelper.GetFilterByNameWhere(userName));
                }
            }

            var distinctRoleNames = new HashSet <string>(dbProperties.CaseInsensitiveComparer);
            var roleWhereBuilder  = PredicateBuilder <Role> .False();

            foreach (var roleName in command.RoleNames)
            {
                if (distinctRoleNames.Add(roleName))
                {
                    roleWhereBuilder.Or(RolesHelper.GetFilterByNameWhere(roleName));
                }
            }

            var userIds = await
                          (
                from u in context.DbContext.Users.Where(userWhereBuilder.Build())
                select u.Id
                          ).ToArrayAsync(cancellationToken).ConfigureAwait(false);

            RequireValid(userIds.Length == distinctUserNames.Count, c => c.UserNames);

            var roleAndUserIdPairs = await
                                     (
                from r in context.DbContext.Roles.Where(roleWhereBuilder.Build())
                from ur in r.Users.DefaultIfEmpty()
                select ValueTuple.Create(r.Id, (int?)ur.UserId)
                                     ).ToArrayAsync <(int RoleId, int?UserId)>(cancellationToken).ConfigureAwait(false);

            var userIdsByRoleId = roleAndUserIdPairs.ToLookup(ur => ur.RoleId, ur => ur.UserId);

            RequireValid(userIdsByRoleId.Count == distinctRoleNames.Count, c => c.RoleNames);

            var userRolesToRemove = new List <UserRole>();

            foreach (var userIdGroup in userIdsByRoleId)
            {
                foreach (var userId in userIds)
                {
                    if (userIdGroup.Any(id => id == userId))
                    {
                        userRolesToRemove.Add(new UserRole {
                            UserId = userId, RoleId = userIdGroup.Key
                        });
                    }
                }
            }

            context.DbContext.UserRoles.RemoveRange(userRolesToRemove);

            await context.DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
        }