想象一下,你已经有了一些看起来像这样的Entity Framework实体(显然不是这些特定的类,而是自动生成的所有实体框架管道的实体;这些只是为了说明):使用LINQ-to-Entities选择父项时排序子对象
public class Parent
{
public int ID { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int ID { get; set; }
public Parent Parent { get; set; }
public int Number { get; set; }
}
我有一个LINQ查询,看起来像这样:
from parent in context.Parents.Include("Child")
select parent
然而,这将返回那里的小朋友都在ID为了父母的列表。我希望孩子们可以在其父母的数字属性中进行排序。 这怎么办?
编辑:澄清:这个想法是让查询隐藏在方法调用(在图层外观)后面,该方法调用简单地返回IList<Parent>
。这使得像匿名类查询和手动排序这样的解决方案变得痛苦(与一些万能解决方案相比,您可以在查询中完成它)。
另外,您无法在无序中读取它们,将它们从EntityCollection中取出,手动对它们进行排序,然后将它们放回到它们的EntityCollection中(当然假设NoTracking处于打开状态)。当你迭代EntityCollection时,它们无论如何都会出错。 :( – 2009-12-11 13:09:55
对,因为'EntityCollection's *无序。* – 2009-12-11 13:30:07