locked
Sorting child entities of parent entities RRS feed

  • Question

  • I have a very complex object graph with multi-levels. I'm not going to detail that but with a simple example as below:

    The example is I have a list of customers and it has a list of orders.

    class Customer
    {
      public string Name { get; set; }
      public string City { get; set; }
      public IEnumerable<Order> Orders { get; set; }
    }
    class Order
    {
      public string OrderNumber { get; set; }
    }

    The following query can return the sorted customers.

    var sortedCustomers = customers.AsQueryable().OrderBy(y => y.Name).ThenBy(z => z.City);

    By using the same query, is it possible to sort the orders for the customers based on the Order Number? I don't see any way to do this except by the following:

    foreach (var customer in sortedCustomerOrders)
    {
    	customer.Orders = customer.Orders.OrderBy(x => x.OrderNumber);
    }
    Any ideas to sort the child entities? As you may have noticed, I'm using LINQ to entities.

    Wednesday, October 3, 2012 9:56 PM

Answers

  • Hi Salescrusade,

    Welcome to the MSDN forum.

    Please try these that works on my side:

        public class Customer
        {
            public Int32 Id { get; set; }
            public String Name { get; set; }
            public String City { get; set; }
            public virtual ICollection<Order> Orders { get; set; }
        }
    
        public class Order
        {
            public String OrderNumber { get; set; }
        }
    
                    var sortedCustomers = context.Customers.AsQueryable().OrderBy(y => y.Name).ThenBy(z => z.City);
                    foreach (var customer in sortedCustomers)
                    {
                        customer.Orders = customer.Orders.OrderBy(x => x.OrderNumber).ToList<Order>();
                    }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:24 AM
    Monday, October 8, 2012 8:49 AM
  • Hi Salescrusade,

    First, I make Orders as virtual, which enable the lazy loading.

    Second, I prefer to use ICollection type as the reference type of property Orders so that I can use ToList<T>() method here:

    customer.Orders = customer.Orders.OrderBy(x => x.OrderNumber).ToList<Order>();

    It works on my side. Please check it.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:24 AM
    Wednesday, October 10, 2012 8:01 AM

All replies

  • Hi Salescrusade,

    Welcome to the MSDN forum.

    Please try these that works on my side:

        public class Customer
        {
            public Int32 Id { get; set; }
            public String Name { get; set; }
            public String City { get; set; }
            public virtual ICollection<Order> Orders { get; set; }
        }
    
        public class Order
        {
            public String OrderNumber { get; set; }
        }
    
                    var sortedCustomers = context.Customers.AsQueryable().OrderBy(y => y.Name).ThenBy(z => z.City);
                    foreach (var customer in sortedCustomers)
                    {
                        customer.Orders = customer.Orders.OrderBy(x => x.OrderNumber).ToList<Order>();
                    }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:24 AM
    Monday, October 8, 2012 8:49 AM
  • Your solution is no different than what I have provided already in my original email.

    It seems either though LINQ to Entities or LINQ to Sql, it is not possible to do a sort operation in second or higher object graph without explicitly doing the sort and setting the member (in this case it is customer.Orders)

    Tuesday, October 9, 2012 10:49 AM
  • Hi Salescrusade,

    First, I make Orders as virtual, which enable the lazy loading.

    Second, I prefer to use ICollection type as the reference type of property Orders so that I can use ToList<T>() method here:

    customer.Orders = customer.Orders.OrderBy(x => x.OrderNumber).ToList<Order>();

    It works on my side. Please check it.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Tuesday, October 23, 2012 8:24 AM
    Wednesday, October 10, 2012 8:01 AM
  • The solution is not different from what I have provided in the original post already though I appreciate your feedback.
    Tuesday, October 23, 2012 10:00 AM