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!
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());