I have two classes:
class Customer
{
    public string Name { get; set; }
    public string ZipCode { get; set; }
    public List<Order> OrderList { get; set; }
}
class Order
{
    public string OrderNumber { get; set; }
}
Using LINQ, i want to get list of Orders group by ZipCode. If Zipcode "12121" has 10 customers and each has 2 orders then it should return me only one Zipcode with the list of 20 orders.
I am trying to do it like this but not able to figure out whats wrong
var orders = br.CustOrderList
.Select(r => new
{
    r.ZipCode,
    r.Name,
    r.OrderList
})
.GroupBy(x => new { x.ZipCode, x.OrderList}); 
Any help please?
This should do what you want:
var orders = br.CustOrderList
    .GroupBy(x => x.ZipCode)
    .Select(g => new
    {
        ZipCode = g.Key,
        Orders = g.SelectMany(x => x.OrderList)
    });
                        var orders = br.CustOrderList
.Select(r => new
{
    r.ZipCode,
    r.Name,
    r.OrderList
})
.GroupBy(x => x.ZipCode); 
You just want to group by the ZipCode so just group by that
Ah yea just try
var order = br.CustOrderList
              .GroupBy(x = x.ZipCode);
No need to select new items out of the list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With