如何对实体框架返回的对象的内部集合进行排序?
public class X
{
public string Field {get; set;}
public EntityCollection<Y> Ys {get; set;}
}
public class Y
{
public string Field {get; set;}
}
from x in entities.Xs
orderby x.Field
select x
有没有办法修改此LINQ查询以返回X对象并对Y对象进行排序?或者我必须在Y列表返回时手动对其进行排序?
修改
此代码必须返回X类型对象的集合,匿名输入不符合当前项目的要求。
答案 0 :(得分:4)
var sortedList = from x in entities.Xs
orderby x.Field
select new {
Field = x.Field,
y = (select y in x.Ys
orderby y.Field
select y)
};
<强>编辑:强> 如果您不想要匿名类型,请执行以下操作:
var sortedList = from x in entities.Xs
orderby x.Field
select new X {
Field = x.Field,
y = (select y in x.Ys
orderby y.Field
select y)
};