Sorting on 2 level deep property.

Posts   
 
    
bjacobs
User
Posts: 73
Joined: 20-Aug-2008
# Posted on: 06-May-2010 18:43:31   

Version 2.6. Adapter.

I am trying to sort an entity collection but it doesn't appear that there is any way to do what I want.

Let's say I have the following class heirarchy. Order -> EntityCollection<OrderDetails> ------>OrderDetailType

NOTE: I want my OrderDetailsCollection to be sorted and remain on the Order.

I have an Order and want to sort the OrderDetails on the name of the OrderDetailType? Is there a way to do this without removing all of the items manually sorting them and then adding them back?

EntityCollection.Sort does not provide the necessary overloads to do what I want even if I implement my own IComparer.

Thanks,

Billy Jacobs

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 06-May-2010 21:45:44   

Are you trying to do this on the server, so that the sort is done using the sql query and the entities are returned to you in from the db in the correct order, or on the client side once you have retrieved the entities...?

Matt

bjacobs
User
Posts: 73
Joined: 20-Aug-2008
# Posted on: 06-May-2010 22:33:24   

Client side once they are retrieved. Reason being is that new items are added on the client and need to resort.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 07-May-2010 05:33:09   

Hi Billy,

This is how you can do that:

  1. Create a custom property that returns the field on the related entity (this is a partial class on your DBGeneric project):
namespace Northwind.Model.EntityClasses
{
    public partial class OrderDetailsEntity
    {
        public string ProductName
        {
            get
            {
                string toReturn = string.Empty;
                if (this.Products != null)
                {
                    toReturn = Products.ProductName;
                }
                return toReturn;
            }
        }
    }
}
  1. Use that property to sort the collection:
order.OrderDetails.Sort("ProductName", ListSortDirection.Ascending, null);

Done wink

David Elizondo | LLBLGen Support Team