Linq groupby query with projection

grouping, linq

Solution

The `result` is an `IGrouping<TKey, T>`, which is itself an `IEnumerable<T>`, so you can do:

List<OrderGroup> set = OrderRepository.GetAllOrders
                      .GroupBy(x => x.CustomerId)
                      .Select(result => new OrderGroup
                      {
                          Orders = result.ToList()
                      }).ToList();

(Note that this assumes `Orders` is assignable from a `List<Order>`.)

Problem

I'm trying to group orders by customer id then project the orders returned (by customer) into a List. Trying to figure out how I would do this? ``` List<OrderGroup> set = OrderRepository.GetAllOrders .GroupBy(x => x.CustomerId).Select(result => new OrderGroup { Orders = ???? //should be all orders from one customer. }).ToList(); ```

Original source