views:

102

answers:

4

I want to make a collection that stores groups of users. A user can be a member of multiple groups, groups can be nested, a selection of groups should return a distinct IEnumerable<user> .

I could write this from scratch of course using the standard List<T>,Dictionary<T,U> etc collections, but is there a built-in collection that already exposes a lot of this behaviour?

Thanks!

A: 

I would have a thought a generic List<T> would work (where T is the class that holds your user).

Dan Diplo
I think he needs a bit more functionality that what is provided by the basic List
instanceofTom
I would have thought the List<T> would hold users and the User class would expose a property that is a Dictionary or HashSet of Groups? Doesn't seem like anything out of ordinary? (But I may be missing something - I often do!)
Dan Diplo
I may just have misread the question, upon rereading (again) I am still uncertain what part of the functionality described is being taken care of in the user class, and what is being handled by the data-structures
instanceofTom
+2  A: 

I would do something like this:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
     List<User> users = new List<User>
     {
      // use a List<T> to hold the users themselves
     };
    }
}

class Group
{
    public Group NestedGroup { get; set; }
}

class User
{
    // Store the groups in a HashSet<T> to keep
    // a distinct list.
    HashSet<Group> groups;

    public IEnumerable<Group> Groups
    {
     get { return groups; }
    }
}

Using a List<T> to hold the users themselves should be fine. Notice that the Groups property of the User type is exposed as an IEnumerable<Group> but is implemented internally as a HashSet<Group> to allow you to keep a distinct list as you wanted.

Andrew Hare
The hashset was useful. It turned out that I did not need nested groups after all. Just categories that could be unioned or intersected. The hashset takes care of that the union or intersection operations will return a IEnumerable of distinct users.I did not like the notion that a User should know about to which categories it belongs, so I did not implement is.
Dabblernl
A: 

I would think that using List<T> would work for saving this information. You could have Groups with a parent group and a list of children groups, and each group containing a list of members, or something similar.

In terms of getting the IEnumerable<User> back out - this should be doable using Enumerable.SelectMany. Take a look at the MSDN sample there - they have a similar thing with pets and owners, where there are multiple elements spanning multiple collections.

Reed Copsey
A: 

It looks like you need a good old relational database. Throw in SQLite and go if you can.

Otherwise...I've sometimes done this via multiple Dictionaries and lists. You have a Dictionary of Groups, each group is a list. Then another Dictionary to store the users by a key of some sorts. Each user can exists in multiple groups that way.

Chris Brandsma