Separate groups of objects based on their properties

Posted by Bevin on Stack Overflow See other posts from Stack Overflow or by Bevin
Published on 2011-01-07T23:48:10Z Indexed on 2011/01/07 23:53 UTC
Read the original article Hit count: 183

Filed under:
|

I want to separate an array of my custom object into several arrays, based on the values of their two properties. The struct looks like this:

struct MyStruct {

    public string Person {
        get;
        set;
    }
    public string Command {
        get;
        set;
    }
}

Now, if I have an array with a few objects:

{Person1, cmd1}
{Person1, cmd3}
{Person2, cmd3}
{Person3, cmd2}
{Person2, cmd4}

I want to be able to place them into one array for each person, that lists all of the commands for that person:

{Person1: cmd1, cmd3}
{Person2: cmd3, cmd4}
{Person3: cmd2}

I hope I've made it clear with my descriptions. I would assume that there is an elegant way to do this with LINQ, but I have no idea where to start.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ