none
How to sort LINQ result of LoadWith RRS feed

  • Question

  • Hi,

     

    I've got a Datacontext with DataLoadOptions like this:

     

    DataLoadOptions lo = new DataLoadOptions();

    lo.LoadWith<Customer>(c => c.Orders);

    db.LoadOptions = lo;

     

    Now I'd like the orders to be sorted (by date) on load, but I can't figure out how to add this to the LoadWith-options.

     

    I tried some stuff like

     

    lo.LoadWith<Customer>(c => c.Orders.OrderBy(c=>c.Date));

     

    but it does not work Sad

     

     

    Or do I need to do something else to sort the EntitySet?

     

    regards,
    Sam

    Friday, June 20, 2008 1:41 PM

Answers

  • Use AssociateWith to do the ordering.  LoadWith only controls loading of the relationship.  AssociateWith controls everything else.

     

    DataLoadOptions lo = new DataLoadOptions();

    lo.LoadWith<Customer>(c => c.Orders);

    lo.AssociateWith<Customer>(c => c.Orders.OrderBy(o => o.Date));

    db.LoadOptions = lo;

     

     

    Sunday, June 22, 2008 7:01 PM
    Moderator

All replies

  • An alternative (that doesn't use LoadOptions) is as follows:

     

    Code Snippet
      var q = from c in db.Customers
              select new { Customer = c, Orders = c.Orders.OrderBy(o => o.OrderDate) };

     

     

     

    Hope that helps.

     

    Thanks,

     

    --Samir

     

    Friday, June 20, 2008 4:24 PM
  • Use AssociateWith to do the ordering.  LoadWith only controls loading of the relationship.  AssociateWith controls everything else.

     

    DataLoadOptions lo = new DataLoadOptions();

    lo.LoadWith<Customer>(c => c.Orders);

    lo.AssociateWith<Customer>(c => c.Orders.OrderBy(o => o.Date));

    db.LoadOptions = lo;

     

     

    Sunday, June 22, 2008 7:01 PM
    Moderator