November Happy Hour will be moved to Thursday December 5th.

Using Query to find users belonging to one of two groups

Vote:
 

I can't really get my UserQuery working as I want. What I want is to select all users belonging to group 1 or 4. 

I should use the GroupsCollectionCriterion but how do I get it to query for group 1 or 4?

 

#38694
Apr 19, 2010 11:32
Vote:
 

Hi,

As you're saying you can use the GroupCollectionCriterion. Here's an example of how you can get all users belonging to group 1 or 4:

            List groupIds = new List{ 1, 4 };

            UserQuery userQuery = new UserQuery();
            userQuery.Groups = new GroupCollectionCriterion();
            userQuery.Groups.Containing = new GroupCriterion();
            userQuery.Groups.Containing.ID = new IntegerCriterion();
            userQuery.Groups.Containing.ID.Includes = new IntegerInCriterion();
            userQuery.Groups.Containing.ID.Includes.Values.AddRange(groupIds);

Karoline

#38709
Apr 20, 2010 12:21
Vote:
 

Thank you Karoline, that did the trick!

If one would like to exclude users of a certain group (8), how do I do that?

It seems that this will not work:

membersQuery.Groups.Containing.ID.Operator = ComparisonOperator.NotEquals;
membersQuery.Groups.Containing.ID.Value = 8;

membersQuery.Groups.Containing.ID.Operator = ComparisonOperator.NotEquals;

 membersQuery.Groups.Containing.ID.Value = 8;

I still get users belonging to 8, if they at the same time belongs to som other group.

 

Jonas

#38712
Apr 20, 2010 13:17
Vote:
 

That was a tricky one, I've tried to find a solution but I ran into the same problems as you: You get the user if they belong to some other group as well.

The only solution I can think of right now should work, but it's not a very good solution:

            UserCollection allUsers = CommunitySystem.CurrentContext.DefaultSecurity.GetUsers();

            UserQuery excludedUserQuery = new UserQuery();
            excludedUserQuery.Groups = new GroupCollectionCriterion();
            excludedUserQuery.Groups.Containing = new GroupCriterion();
            excludedUserQuery.Groups.Containing.ID = new IntegerCriterion();
            excludedUserQuery.Groups.Containing.ID.Value = 593;

            UserCollection excludedUsers = QueryHandler.GetQueryResult(excludedUserQuery);

            // Get all users who are not in the excludedUsers UserCollection
            IEnumerable users = from u in allUsers where !excludedUsers.Contains(u) select u;

I'll think a little bit about it to see if I can come up with a better one :)

Karoline

 

#38738
Apr 20, 2010 15:56
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.