none
Selecting sets of data RRS feed

  • Question

  • Hi all,

    I've got a data model with a column that I'd like to group by and aggregate the remaining data by into a set.  As in, if my data of type MyType were:

    GroupByThis Column2  Column3
    First 1 2
    First 3 5
    Second 2 4
    Second 8 16
    First 8 13
    Second 32 64
    I'd want to get an IEnumerable with a GroupByThis string property and another column of type IEnumerable<MyType> property containing all of the entities with the same GroupByThis property.

    Is this possible?
    Friday, January 29, 2010 5:06 PM

Answers

  • Of course, I could just answer my own question.  In case anyone else is interested in the answer, here it is.

    struct Temp
    {
        public String GroupByThis;
        public IEnumerable<MyType> MyTypes;
    }
    
    IEnumerable<Temp> data = from m in db.MyTypes
                      group m by m.GroupByThis into g
                      orderby g.Key
                      select new Temp { GroupByThis = g.Key, MyTypes = g };
    
    foreach (Temp i in data)
    {
        Console.Out.WriteLine(i.GroupByThis );
        foreach (MyType m in i.MyTypes)
        {
            Console.Out.WriteLine("    " + m.Column1 + ", " + m.Column2);
        }
    }
    • Marked as answer by Don 01001100 Friday, January 29, 2010 5:21 PM
    • Unmarked as answer by Don 01001100 Friday, January 29, 2010 5:22 PM
    • Marked as answer by Don 01001100 Friday, January 29, 2010 5:22 PM
    Friday, January 29, 2010 5:21 PM