LINQ查询 - 如何排序和过滤热心的提取 [英] LINQ Query - how sort and filter on eager fetch

查看:140
本文介绍了LINQ查询 - 如何排序和过滤热心的提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 过滤一个子字段

  2. 对父母和孩子进行排序

  3. 返回一个列表或父母与孩子预先填充

如果我尝试

  from p in _context.Parents.Include(children)
将c加入_context.childrenon p.Id等于c.ParentId
其中d.DeletedDate == null
orderby p.Name升序,c.Name
选择p

然后我收到了父对象,但每个父对象都有NULL



如果我尝试

  from p in _context.Parents.Include(children)
orderby p.Name ascending
选择p

查询返回所有的父项和子节点,但不会过滤或排序。



我想要的结果是一个 IEnumerable< Parent>
ie

 父[0] .name = foo
父[0] .children = IEnumerable< Child>
父[1] .name =bar
父[1] .children = IEnumerable< Child>


解决方案

解决方案取决于您正在尝试做什么。



第一个查询给人的印象是想将对象中的结果展平(像这样(伪代码,我希望很清楚我的意思)): / p>

  {Parent1,Child1} 
{Parent1,Child2}
{Parent1,Child3}
{Parent2,Child1}

在这种情况下,每个结果row将是一个 Parent Child 属性,您可以按父名称排序,然后按子名排序。



第二个查询只返回 Parent 对象,(你不显示它,但是我认为EF被指示这样做)每个有一个儿童集合。在这种情况下,您只能按父名字排序;如果要对每个 Parent 的孩子进行排序,请对该对象的 Children 集合进行排序。



您要做哪两个?



更新



好的,好像你想要第二个。我不相信可以直接做到。您可以在枚举结果时执行此操作 - 由于父级已经排序,只需对每个孩子进行排序:

  var sortedChildren = parent.Children.OrderBy(c => c.Name); 


How do I do a eager query of a parent child relationship that:

  1. filters a on child fields
  2. sorts on both parent and child
  3. return a List or Parents with the children pre-populated

If I try

from p in _context.Parents.Include("children")
join c in _context.childrenon p.Id equals c.ParentId 
where d.DeletedDate == null
orderby p.Name ascending, c.Name 
select p

Then I get the Parent object back but each Parent has NULL for children

if I try

from p in _context.Parents.Include("children")
orderby p.Name ascending
select p

The query returns all Parents and children but they are not filtered or sorted.

The result I want back is a IEnumerable<Parent> i.e.

Parent[0].name = "foo"
Parent[0].children = IEnumerable<Child>
Parent[1].name = "bar"
Parent[1].children = IEnumerable<Child>

解决方案

The solution depends on what exactly you are trying to do.

The first query gives the impression that you want to "flatten out" the results in objects, like this (pseudocode, I hope it's clear what I mean):

{ Parent1, Child1 }
{ Parent1, Child2 }
{ Parent1, Child3 }
{ Parent2, Child1 }

In this case each result "row" would be an object having a Parent and a Child property, and you could sort by parent name and then by child name.

The second query just returns the Parent objects and (you don't show it but I assume EF has been instructed to do that) each one has a Children collection. In this case you can only sort by parent name; if you want to sort each Parent's children, sort the Children collection on that object by itself.

Which of the two do you want to do?

Update

OK, it seems you want the second one. I don't believe it can be done directly. You can just do it when you enumerate the results - since the Parents are already sorted, simply sort each one's children:

var sortedChildren = parent.Children.OrderBy(c => c.Name);

这篇关于LINQ查询 - 如何排序和过滤热心的提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆