有没有一种方法可以从子集合中定义默认订单列?就我而言,我有一个Form实体,该实体具有FormItem实体的集合,称为FormItems。 FormItem具有称为DisplayOrder(int)的属性。我想确保从方法返回的所有Form实体都正确地排序了该集合。有没有办法在返回结果之前执行此操作?
例如,我尝试了此操作,但列表实际上未排序:
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;
有没有不使用DataLoadOptions的方法?
最佳答案
尝试这个:
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
);
含义,通过
DataLoadOptions.AssociateWith
方法对子项进行排序。关于c# - Linq to SQL有序子集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2601704/