Linq to SQL ordered child collection

Posted by Matt Connolly on Stack Overflow See other posts from Stack Overflow or by Matt Connolly
Published on 2010-04-08T16:30:17Z Indexed on 2010/04/08 16:33 UTC
Read the original article Hit count: 385

Filed under:
|

Is there a way to define a default order column from a child collection? In my case, I have a Form entity that has a collection of FormItem entities called FormItems. FormItem has a property called DisplayOrder (int). I want to make sure that any Form entities I return from a method have that collection properly ordered. Is there a way to do this before returning the result?

For example, I tried this but the list is not actually sorted:

var form = context.Forms.FirstOrDefault(x => x.IsDeleted == false && x.FormName == formName);
if (form != null)
{
    form.FormItems.OrderBy(x => x.DisplayOrder);
    // I can't even figure out a way to cast this next line so that it will compile
    // form.FormItems = form.FormItems.OrderBy(x => x.DisplayOrder);
}
return form;

Is there a way to do this without using DataLoadOptions?

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about c#