Sorting Child Collections In Entity Framework

I made a blog post earlier, where I discussed how to use AssociateWith method on DataLoadOptions to sort child collections on SQL server before they are loaded into memory. This option is only available in linq to SQL. In entity framework, in order to accomplish sorting on child collection, you have to use CreateSourceQuery method. CreateSourceQuery returns an objectquery that gets converted to SQL and send to the database. So if you want your child collections to be loaded differently such as sorted in a different order or apply filter, than you have to get access to ObjectQuery instance that is responsible for loading the child subcollection and modify the query to add ordering before ObjectQuery gets executed.  CreateSourceQuery query method is available on EntityCollection and EntityReference.

Below is an example that shows how to do that

var context = new NorthwindEntities();
var cus1 = context.Customers.Single(c => c.CustomerID == "ALFKI");
foreach (var order in cus1.Orders.CreateSourceQuery().OrderBy(o => o.ShipCity)
{
       Console.WriteLine(order.ShipCity);
}

Thanks Danny for the help.

1 Comment

  • The way you have suggested this works, but I believe it will hit the database again and requires the data context to be open still. I think an easier whay is to do

    cus1.Orders.OrderBy(o => o.ShipCity)

    I just did this an it worked very nicely.

    David
    Twitter:CTRLALT313373

Comments have been disabled for this content.