0

What i am trying to do is i have sales order object which contains

sales order header
and list of order lines

within the order lines i have the actual order line, product information object and stock information object:

public class SalesOrder
{
  public Header SalesHeader { get; set; }
  public List<OrderLineProductInfo> OrderLines { get; set; }
}

public class OrderLineProductInfo
{
  public Line salesOrderLine { get; set; }
  public Info ProductInfo { get; set; }
  public Stock ProductStock { get; set; }
}

so i can get a list of SalesOrder Objects so example sales order index 0

has 2 lines the ProductStockObject within one of these lines has Preferred Supplier of abc and the other line has Preferred Supplier 123

i want to be able to group on the Preferred Supplier property

var separatePreferredSuppliers =
     (from b in x.OrderLines
                 .GroupBy(g => g.ProductStock.PreferredSupplier ) 
                   select ...
     ).ToList();

not quite sure what comes next what needs to be selected? a new list of SalesOrder?

I want it so that it gives two instances of the sales order but split in 2 one for each preferred supplier

Hogan
  • 69,564
  • 10
  • 76
  • 117
Houlahan
  • 783
  • 3
  • 19
  • 46
  • This question looks to be what you're after, see here: http://stackoverflow.com/questions/7325278/group-by-in-linq?rq=1 – Iain Ward Feb 23 '15 at 16:01

2 Answers2

0

I think I get what you mean

from line in x.OrderLines
group line by line.ProductStock.PreferredSupplier into grouped
select new SalesOrder
{
     OrderLines = grouped.ToList()
}

though I'm not sure how you populate your SalesHeader

dav_i
  • 27,509
  • 17
  • 104
  • 136
0

easy when you know what the groupby function ruturns -- you want the key and the list:

.Select( (g) => new { supplier = g.Key, prodlist = g.ToList()}
Hogan
  • 69,564
  • 10
  • 76
  • 117