tags:

views:

79

answers:

4

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?

A: 

First, Make FormItems private and change name to PrivateFormNames in designer.

Than you should be able to declare:

partial class Form
{
    public IQueryable<FormItem> FormItems
    { 
        get { return PrivateFormItems.OrderBy(x => x.DisplayOrder); } 
    }
}

I will think how you can make it virtual... Something like this: You can create Derived class for Form in linq digram, and set inheritance in that way, that it always create derived Form class (You can specify inheritence default).

Than you can mark FormItems as virtual and override it in derived class:

 partial class FormDerived
    {
        public override IQueryable<FormItem> FormItems
        { 
            get { return base.FormItems.OrderBy(x => x.DisplayOrder); } 
        }
    }

and ALL linq and application will start to use ordered list. Only option to get it unordered will be to use context.FormItems.

Sergey Osypchuk
Its not the forms I need ordered but the child collection, FormItems
Matt Connolly
I suggested another option. It is possible to make it even better with override but it will be more complex. Please let me know if you need such help
Sergey Osypchuk
Inheritence option added
Sergey Osypchuk
A: 

You could add an additional property to your Form entity trough a partial class that would return the ordered FormItems.

partial class Form
{
    public IQueryable<FormItem> OrderedFormItems
    { 
        get { return FormItems.OrderBy(x => x.DisplayOrder); } 
    }
}
bruno conde
True, but I would rather not create a custom property. It would be nice if the code that calls this is ignorant of the need to sort, and shouldn't have to worry about using OrderedFormItems instead of FormItems. If Linq can't handle what I want this is probably the best solution though.
Matt Connolly
A: 

what about:

var form2 = form.FormItems.OrderBy(x => x.DisplayOrder); return form2;

Robert Ivanc
A: 

Try this:

DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith<Form>(f => f.FormItems);
loadOptions.AssociateWith<Form>(f => 
    f.FormItems
        .OrderBy(fi => fi.DisplayOrder);
);

context.LoadOptions = context;

var form = context.Forms
    .FirstOrDefault(x => 
        !x.IsDeleted &&
        x.FormName == formName
    );

meaning, ordering of child items made by DataLoadOptions.AssociateWith method.

Alex