c# - Group Number in a LINQ Group by Query -


I am using LINQ to wet my feet. It has been a good first resource, but I'm not getting an example of what I currently need.

I just need to associate the sequential group number with each group. I have a working solution:

  group from group = c to new list in c.Name descriptions in group c {name = details.Key, DetailRecords = details}; Int groupNumber = 0; Foreach (different groups in groups) {// // Process of each group and its record ... // groupNumber ++; }  

But, I believe that it is possible to use LINQ to generate a group number I

It depends on your exact requirements, but you can use it:

  var groupArray = groups.ToArray ();  

Similarly, you can use toList . These data structures are sequential, and each group has an index.


If you need an object on the object, then another option is to use Select

  list.GroupBy ( C = & gt; c.Name). Select ((Description, Ind) => New {Name = Description, Extension Description = Description, Index = Ind});  

Comments