Print Page | Close Window

Lazy load sort order

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2872
Printed Date: 16-Aug-2026 at 8:16am


Topic: Lazy load sort order
Posted By: Togas
Subject: Lazy load sort order
Date Posted: 08-Aug-2011 at 7:23am
Is there a way to set the sort order when you are using lazy loading?



Replies:
Posted By: sbelini
Date Posted: 09-Aug-2011 at 3:45pm
Hi Togas,
 
Try
 
aCustumer.Orders.OrderBy(ord => ord.Freight)
 
Regards,
   Silvio.


Posted By: Togas
Date Posted: 10-Aug-2011 at 7:16am
Silvio,

Where would I put that?  My initial thought was that should be part of my initial load query but my child table isn't an option when I query the parent table and I also noticed you are only referencing a single customer.

The 2 tables are linked in my model


Posted By: sbelini
Date Posted: 10-Aug-2011 at 11:39am
Hi Togas,
I assume that if you want to sort the order of a navigation property (i.e. lazy loading) you are probably binding it to something.
If you want to load related entities during your initial load query, then you will need to use .Include:
 
var initialLoadQuery = mgr.Customer
                          .Include("Orders");
 
in the case above, Orders are not being ordered because it's not bound to anything. (it was simply loaded in the EntityCache using eager loading)
 
Could you explain in detail what you are trying to accomplish, so I can better understand your issue?
 
Regards,
   Silvio.


Posted By: Togas
Date Posted: 10-Aug-2011 at 11:50am
Your assumption was correct, I'm binding to a datagrid.  It's a basic Master-Detail type page.






<sdk:DataGrid AutoGenerateColumns="False" 
ItemsSource="{Binding SelectedOrder.OshkoshEquipments}"
SelectedItem="{Binding SelectedEquipment, Mode=TwoWay}">
...

</sdk:DataGrid>


Here is the relevant code from my view model

Public Property OshkoshOrders As ObservableCollection(Of Oshkosh)
Private Sub LoadData
      OshkoshOrders = New ObservableCollection(Of Oshkosh)

      Dim qOrders = _mgr.Oshkoshes
      qOrders.ExecuteAsync(Sub(o)
                             If o.HasError Then
                               Throw New InvalidOperationException("Query failed")
                             End If
                             o.Results.ForEach(Sub(oe)
                                                 OshkoshOrders.Add(oe)
                                               End Sub)

                             SelectedOrder = OshkoshOrders.FirstOrDefault
                           End Sub)
End Sub



Posted By: sbelini
Date Posted: 15-Aug-2011 at 12:17pm
Hi Togas,
 
Since you're doing the binding to the DataGrid in the XAML, you will need to use a CollectionViewSource. There you can set the sort order there and then bind it to your DataGrid.
 
Take for instance our Silverlight Tour of DevForce sample (in the http://drc.ideablade.com/xwiki/bin/view/Documentation/tour-devforce-silverlight-part1 - DevForce Resource Center ).
 
Instead of binding the Orders of currentEmployee directly into the DataGrid
 
<sdk:DataGrid Name="EmployeeOrdersGrid"
              ItemsSource="{Binding ElementName=EmployeeDataForm,
                                    Path=CurrentItem.Orders}" />
 
 
you'd bind the Orders to the CollectionViewSource, sort it, and bind the CollectionViewSource to the DataGrid
 
 
<Grid.Resources>
  <CollectionViewSource x:Key="OrdersViewSource" Source="{Binding ElementName=EmployeeDataForm,
                                                                   Path=CurrentItem.Orders}">
    <CollectionViewSource.SortDescriptions>
      <scm:SortDescription PropertyName="Freight"/>
    </CollectionViewSource.SortDescriptions>
  </CollectionViewSource>
</Grid.Resources>
 
...
 
<sdk:DataGrid Name="EmployeeOrdersGrid"
              ItemsSource="{Binding Source={StaticResource OrdersViewSource}}"
/>
 
Regards,
   Silvio.


Posted By: Togas
Date Posted: 15-Aug-2011 at 12:44pm
Perfect

Thanks Silvio



Print Page | Close Window