none
How to sort parent by child's property RRS feed

  • Question

  • I’m using Entity Framework to load entities from database. One of the requirement here is whiling retrieving entities I want to sort Orders by Customer.Orders.OrderBy(x => x.OrderDetails.SingleOrDefault().Number);

    Note that in my case there will be only one OrderDetail per Order.

    Below is the customer repository used to get customer hierarchy

      

    return repository.GetQuery() .Where(x => x.CustomerID == customerID) .Include(x => x.Orders.Select(y => y.OrderDetails)) .Include(x => x.Orders.Select(y => y.Payments)) .SingleOrDefault();

    // how do i sort here

    public class Customer
        {
            public IList<Order> Orders { get; set; }
        }
    
        public class Order
        {
            public IList<OrderDetail> OrderDetails { get; set; }
    
            public IList<Payment> Payments { get; set; }
        }
    
        public class OrderDetail
        {
            public int Number { get; set; }
        }
    
        public class Payment
        {
    
        }

                  

    
    
    
    
                

    • Edited by lax4u Tuesday, September 18, 2012 7:31 PM
    Tuesday, September 18, 2012 7:30 PM

All replies

  • Hi lax4u;

    This query only returns one record by the use of the SingleOrDefault method on the end so there is nothing to sort, or am I missing something.

    return repository.GetQuery()
     .Where(x => x.CustomerID == customerID)
     .Include(x => x.Orders.Select(y => y.OrderDetails))
     .Include(x => x.Orders.Select(y => y.Payments))
     .SingleOrDefault();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, September 18, 2012 8:38 PM
  • the repository is of type customer, it should return single customer. But single customer has collection of orders which needs to be sorted by OrderDetails.Number

    Also 

    repository.GetQuery() returns IQueryable<Customer>


    • Edited by lax4u Tuesday, September 18, 2012 9:40 PM
    Tuesday, September 18, 2012 9:17 PM
  • Hi lax4u;

    You state that GetQuery returns a single Customer with a list of Orders which you want to order. The order of entities in the object context is set by the order in which the server returned them from the query. So what you will need to do is to create a list of Orders separate from the Customer entity that is sorted in the order you need them to be.

    // Get the single instance of the Customer entity
    var customer = repository.GetQuery() ... ;
    
    // Sort the Orders Entities
    List<Orders> orders = customer.Orders.OrderBy( x => x.OrderDetails.SingleOrDefault().Number).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, September 19, 2012 3:57 AM
  • GetCustomer  returns IQueryable<Customer>
    Wednesday, September 19, 2012 2:54 PM
  •   

    No where in your posts do you say anything about GetCustomer, I see a GetQuery.

    So what is your question?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, September 19, 2012 5:36 PM