How do you sort a collection by the collections child collection??

Domanda How do you sort a collection by the collections child collection??

  • giovedì 2 agosto 2012 14:37
     
     

    Hi,

    Confused yet???

    Ok, I have a collection of Orders and each of the Order objects in the collection have a OrderLines collection. The OrderLines collecting contains OrderLine orders.

    What I want to do is get a collection of Orders sorted by the Order.OrderLines collection. The idea is to print them ordered by product code, so that the order with the same product code are printed together.

    I get the feeling this can be done in LINQ, but I have no experiance with it.

    Thank you for any help,

    Craig

Tutte le risposte

  • giovedì 2 agosto 2012 15:41
     
      Contiene codice

    Might be something like this:

    orders.SelectMany( order => order.orderLines ).GroupBy( orderline => orderline.productCode )

    e.g.:

    public class OrderLine
    {
        public int productCode;
        public override string ToString()
        {
            return string.Format( "OrderLine productCode = {0}", productCode );
        }
    }
    public class Order
    {
        public List<OrderLine> orderLines = new List<OrderLine>();
    }
    class Program
    {
        static void Main( string[] args )
        {
            List<Order> orders = new List<Order>() {
                new Order {
                    orderLines = new List<OrderLine>() {
                        new OrderLine() { productCode = 1 },
                        new OrderLine() { productCode = 2 }
                    }
                },
                new Order {
                    orderLines = new List<OrderLine>() {
                        new OrderLine() { productCode = 1 },
                        new OrderLine() { productCode = 1 },
                        new OrderLine() { productCode = 2 },
                        new OrderLine() { productCode = 3 }
                    }
                },
                new Order {
                    orderLines = new List<OrderLine>() {
                        new OrderLine() { productCode = 2 }
                    }
                }
            };
            var query = orders.SelectMany( order => order.orderLines ).GroupBy( orderline => orderline.productCode );
            foreach( var collection in query )
            {
                Console.WriteLine( collection.Key );
                foreach( var item in collection )
                {
                    Console.WriteLine( item );
                }
            }
        }
    }
    

  • giovedì 2 agosto 2012 16:07
     
      Contiene codice

    A bit different:

        class Program
        {
            static void Main(string[] args)
            {
                List<Order> orders = new List<Order>();
    
                //order 1:
                Order order1 = new Order();
                order1.OrderID = 1;
                order1.OrderTime = new DateTime(2012, 4, 14);
                Product p1 = new Product { ProductID = 1, ProductName = "Product 1", ProductPrice = 13.44m };
                Product p2 = new Product { ProductID = 2, ProductName = "Product 2", ProductPrice = 34.21m };
                order1.AddProduct(p1);
                order1.AddProduct(p2);
    
                //order 2:
                //order 1:
                Order order2 = new Order();
                order2.OrderID = 2;
                order2.OrderTime = new DateTime(2012, 6, 21);
                Product p3 = new Product { ProductID = 2, ProductName = "Product 2", ProductPrice = 34.21m };
                Product p4 = new Product { ProductID = 4, ProductName = "Product 4", ProductPrice = 53m };
                Product p5 = new Product { ProductID = 2, ProductName = "Product 2", ProductPrice = 34.21m };
                order2.AddProduct(p3);
                order2.AddProduct(p4);
                order2.AddProduct(p5);
    
                //add order to collection of orders:
                orders.Add(order1);
                orders.Add(order2);
    
                //now lets sort order by 
                var query = orders.SelectMany(o => o.products).GroupBy(g => g.ProductID).ToList();
                foreach (var items in query)
                {
                    Console.WriteLine(string.Format("Order number {0}:", items.Key));
                    foreach(var item in items)
                    {
                        Console.WriteLine(string.Format("Product id: {0}, product name: {1}, Product price: {2}",
                            item.ProductID, item.ProductName, item.ProductPrice));
                    }
                    Console.WriteLine("");
                }
                Console.ReadLine();
            }
        }
    
        class Order
        {
            public int OrderID { get; set; }
            public DateTime OrderTime { get; set; }
            public List<Product> products;
    
            public Order()
            {
                products = new List<Product>();
            }
    
            public void AddProduct(Product p)
            {
                products.Add(p);
            }
    
            public List<Product> GetProducts()
            {
                return products;
            }
        }
    
        class Product
        {
            public int ProductID { get; set; }
            public string ProductName { get; set; }
            public decimal ProductPrice { get; set; }
        }


    Mitja

  • venerdì 3 agosto 2012 06:56
     
     

    Hi,

    Thank you for the ideas, I have started reading about LINQ to better understand what it can do.

    Something I am coming up against though. It I have the Orders collection full of orders, can I run this kind of LINQ query and return an Orders collection with the sorted Order objects by Order.OrderLines?

    I am finding that I can't just cast a var directly to my object after a query.

    Does that make sense? I'm starting to confuse myself now...

    Craig

  • sabato 4 agosto 2012 00:17
     
     

    Hi,

    Thank you for the ideas, I have started reading about LINQ to better understand what it can do.

    Something I am coming up against though. It I have the Orders collection full of orders, can I run this kind of LINQ query and return an Orders collection with the sorted Order objects by Order.OrderLines?

    I am finding that I can't just cast a var directly to my object after a query.

    Does that make sense? I'm starting to confuse myself now...

    Craig

    You can certainly order your orders collection.  In general it would be orders.OrderBy( something );

    So you have to figure out what the something is.  You want to order them by Order.OrderLines.  Which as I understand it is a collection of objects of type OrderLine.  This is possible if you can define which order should come first.  You originally said you want to order them by product code, but if I understand correctly, then each OrderLine could have a different product code, so it's not clear to me how to sort the orders because each consists of a bunch of product codes.  Did I miss something?

    Here's what I'm picturing based on what you said so far:

    Order #1 { Product codes of the OrderLines contained within: {3, 9, 10, 44 } }

    Order #2 { Product codes of the OrderLines contained within: {5, 9, 11, 16 } }

    So which comes first?  Order #1 or Order #2???

    If I misunderstand the structure of your data, then please clarify.

  • sabato 4 agosto 2012 04:23
     
     

    post your format of Data, then we may assist you better.

    Probably you should use group by to group the collections based on product code.

    orderby to sort the collections based on order.orderlines

    Happy Programming!!!

  • lunedì 6 agosto 2012 15:17
     
     

    Ok, let me see if I can clear this up..

    The Orders collection (custom collection) contains Order objects, each Order contains a collection OrderLines, which in turn contain the OrderLine objects. Those OrderLine objects has a property ProductSKU, that contains text with the product sku in.

    Orders > Order > OrderLines > OrderLine
                                                   OrderLine
                  Order > OrderLines > OrderLine
                                                   OrderLine

    I want to orders collection to be sorted so that the order objects inside are ordered by the productsku. It fine to only sort by the first OrderLine, each order has a min of one line, and sometimes more.

    It will just make printing easier because the orders will be printed in order of the first product line.

    Thank you