简体   繁体   English

使用 LINQ-to-Entities 选择父对象时对子对象进行排序

[英]Sort child objects while selecting the parent using LINQ-to-Entities

Imagine you've got some Entity Framework entities that look like this (obviously not these specific classes, but the autogenerated ones with all the Entity Framework plumbing; these are just for illustration):想象一下,您有一些 Entity Framework 实体看起来像这样(显然不是这些特定的类,而是带有所有实体框架管道的自动生成的类;这些只是为了说明):

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; }
}

I have a LINQ query that looks like this:我有一个如下所示的 LINQ 查询:

from parent in context.Parents.Include("Child")
select parent

However, this returns a list of Parents where the children are in ID order.但是,这将返回子项按 ID 顺序排列的父项列表。 I want the children to be sorted by their Number property within their Parent.我希望孩子们在他们的父级中按他们的 Number 属性排序。 How can this be done?如何才能做到这一点?

Edit: A clarification: the idea is to have the query hidden behind a method call (in the layer facade) that simply returns an IList<Parent> .编辑:澄清:这个想法是将查询隐藏在方法调用(在层外观中)后面,该方法调用只返回IList<Parent> This makes using solutions like anonymous class queries and manual sorting painful (compared to some panacea solution where you can just do it in the query or something).这使得使用匿名类查询和手动排序等解决方案很痛苦(与某些万能解决方案相比,您可以在查询或其他方面进行操作)。

Alex James discusses this issue in this tip . Alex James 在这个技巧中讨论了这个问题。

Essentially, relationships are considered as unordered, per standard relational modeling.本质上,根据标准关系建模,关系被认为是无序的。 So you can't get them sorted.所以你不能对它们进行排序。 But you can project onto other collections, which can be sorted.但是您可以投影到其他可以排序的集合上。

Take a look at this post .看看这个帖子 You could try something like this:你可以尝试这样的事情:

var query = ((from parent in context.Parents
              from child in parent.Child
              orderby child.Number ascending
              select parent) as ObjectQuery<Parent>
            ).Include("Child");

One option is executing the query and sorting in memory (eg on output).一种选择是在内存中执行查询和排序(例如在输出上)。

var parents = context.Parents.Include("Child").ToList(); //note that ToList is here just to execute the query and get the objects in memory

foreach (var p in parents)
{
   //do something with parent item
   foreach (var c in p.Child.OrderBy(c => c.Number))
   {
      /do something with the child item
   }
}

There are two other options that also seem to work with their own pros and cons:还有另外两个选项似乎也各有优缺点:

LINQ ".Include" orderby in subquery 子查询中的LINQ“.Include”orderby

LINQ OrderBy Name ThenBy ChildrenCollection.Name LINQ OrderBy Name ThenBy ChildrenCollection.Name

here is something that I have done:这是我做过的事情:

var query = from parent in context.Parents 
            select new 
            {
                 parent,
                 childs = from child in context.Child
                            orderby child.ID ascending
                            select new
                            {
                                 child
                            }
            }

I implememented something like this and it worked very well for me我实现了这样的东西,它对我来说效果很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM