SortDescription on related entity

Posts   
 
    
mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 17-Feb-2009 21:30:26   

I am building a WPF grid right now and am trying to sort it on a property of a M:1 entity that the grid is bound to. Microsoft has many examples of using a SortDescription in the dataview that the grid is bound to in order to accomplish this, but I can't figure out how to set this sort description equal to the property of a related entity...

So, say I have a list of OrderItem entities. Each OrderItem has an Order entity attached. The list of OrderItem entities is bound to a wpf listview (with a gridview inside). I know I can sort this gridview by the OrderItem.Id property with something like:


ICollectionView dataView = CollectionViewSource.GetDefaultView(TheListView.ItemsSource);
dataView.SortDescriptions.Add( New SortDescription("Id", ListSortDirection.Ascending);
dataView.Refresh();

However, I want to sort by the name of the Order entity...I know I can add a field based on a related entity to the OrderItem entity, but I would prefer not to do this as I would be littering the entities with GUI properties.

Is there a way to do this with something like:


ICollectionView dataView = CollectionViewSource.GetDefaultView(TheListView.ItemsSource);
dataView.SortDescriptions.Add( New SortDescription("Order.Name", ListSortDirection.Ascending);
dataView.Refresh();

where "Order" is the name of the related entity property?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Feb-2009 03:21:34   

I afraid that wont work. You at least have to add a Custom Property on your OrderItem entity (OrderName, for example) that access the related entity field, then you could sort on that property using Entity Property. Other thing you can do (if you have .Net 3.5 thus LinqToObjetcs) is using LinqToObjects to sort the underlying list in-memory.

David Elizondo | LLBLGen Support Team