score:9

Accepted answer

this depends on your exact needs, but you can use:

var grouparray = groups.toarray();

similarly, you can use tolist. these data structures are sequential, and each group has an index.


if you do need the index on the object you create, another option is to use select:

list.groupby(c => c.name)
    .select((details, ind) =>
    new
    {
        name = details.key,
        detailrecords = details,
        index = ind
    });

score:1

if it's just a sequential group number, just use the count() method on your ienumerable.

var groups =
   from c in list
   group c by c.name into details
   select new {name = details.key, detailrecords = details};

for(int i = 0; i < groups.count(); i++)
{
  //process records
}

then, if you need the specific group number, you can just grab i.

score:6

this should do the trick:

int groupnumber = 0;
var groups =
   from c in list
   group c by c.name into details
   select new { name = details.key, detailrecords = details, grpnum = groupnumber++};

Related Query