I have this structure
Customer
- has many Orders
- has many OrderItems
I want to generate a list of CustomerItems
via LINQ give
you can achive it with group join
var result = (from c in Customers
join oi in OrderItems on c.Id equals oi.Order.Customer.Id into g
Select new { customer = c, orderItems = g});
c is Customer and g is the customers order items.
I think you want:
items.GroupBy(item => item.Order.Customer)
.Select(group => new { Customer = group.Key, Items = group.ToList() })
.ToList()
If you want to continue use the overload of GroupBy
you are currently using, you can do:
items.GroupBy(item => item.Order.Customer,
(key, group) => new { Customer = key, Items = group.ToList() })
.ToList()
...but I personally find that less clear.
you may also like this
var Grp = Model.GroupBy(item => item.Order.Customer)
.Select(group => new
{
Customer = Model.First().Customer,
CustomerId= group.Key,
Orders= group.ToList()
})
.ToList();