How do I iterate an IGrouping Interface?

The IGrouping<TKey, TElement> interface inherits IEnumerable<TElement>:

foreach (var group in groupedData)
{
    var groupKey = group.Key;
    foreach (var groupedItem in group)
        DoSomethingWith(groupKey, groupedItem);
}

I note that you will be better off using this for your query, however:

var groupedData = Data.GroupBy(x => x.Period); 

rather than this:

var groupedData = Data.GroupBy(x => new {x.Period}); 

If, for example, you wanted to average the adjustments, you could do this:

foreach (var group in groupedData)
    Console.WriteLine("Period: {0}; average adjustment: {1}", group.Key, group.Average(i => i.Adjustment));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)