Get "Value" property in IGrouping

fearofawhackplanet picture fearofawhackplanet · Jan 26, 2011 · Viewed 92.5k times · Source

I have a data structure like

public DespatchGroup(DateTime despatchDate, List<Products> products);

And I am trying to do...

var list = new List<DespatchGroup>();

foreach (var group in dc.GetDespatchedProducts().GroupBy(i => i.DespatchDate))
{
    // group.Values is not correct... how do I write this?
    list.Add(new DespatchGroup(group.Key, group.Values);
}

I'm obviously not understanding IGrouping as I can't see how to actually get to the data records within the group!

Answer

Marc Gravell picture Marc Gravell · Jan 26, 2011

The group implements IEnumerable<T> - In the general case, just call foreach over the group. In this case, since you need a List<T>:

list.Add(new DespatchGroup(group.Key, group.ToList());