views:

38

answers:

1

Hi, I have a list of data structured like this:

Field1  Field2  Field3
1   1   "abc"
1   1   "def"
1   2   "123"
1   2   "456"

I want to be able to merge this data so I end up with a single record per Field1 & Field2 and with the Field3 data concatenated. So in the above case I would get:

Field1  Field2  Field3
1   1   "abcdef"
1   2   "123456"

I would like to use linq/query expressions for this.

+3  A: 
var query =
    from row in rows
    group row by new { row.Field1, row.Field2 } into g
    select new RowClass
    {
        Field1 = g.Key.Field1,
        Field2 = g.Key.Field2,
        Field3 = g.Aggregate(
                    new StringBuilder(), 
                    (sb, grp_row) => sb.Append(grp_row.Field3))
                  .ToString()
    }
Ben M
Thanks! I didn't realise you could group on multiple fields. I was trying to do nested grouping and just getting a mess.
Glad to help. I agree that it's not a very intuitive syntax.
Ben M