简体   繁体   English

LINQ - 在父子层次结构中选择全部

[英]LINQ - Select all in parent-child hierarchy

I was wondering if there is a neat way do to this, that DOESN'T use any kind of while loop or similar, preferably that would run against Linq to Entities as a single SQL round-trip, and also against Linq To Objects. 我想知道是否有一个简洁的方法做到这一点,不要使用任何类型的while循环或类似的,最好是针对Linq to Entities作为单个SQL往返运行,并且还针对Linq To Objects。

I have an entity - Forum - that has a parent-child relationship going on. 我有一个实体 - Forum - 正在进行亲子关系。 That is, a Forum may (or in the case of the top level, may not) have a ParentForum , and may have many ChildForums . 也就是说, Forum可以(或者在顶层的情况下,可能没有)拥有ParentForum ,并且可能有许多ChildForums A Forum then contains many Posts . Forum然后包含许多Posts

What I'm after here is a way to get all the Posts from a tree of Forums - ie the Forum in question, and all it's children, grandchildren etc. I don't know in advance how many sub-levels the Forum in question may have. 我在这里得到的是一种从Forums树中获取所有Posts的方法 - 即有问题的Forum ,以及它的所有孩子,孙子等等。我事先并不知道Forum多少子级别可能有。

(Note - I know this example isn't necessarily a valuable use case, but the Forum object model one is one that is familiar to most people, and so serves as a generic and accessible premise rather than my actual domain model.) (注意 - 我知道这个例子不一定是一个有价值的用例,但是Forum对象模型是大多数人都熟悉的,因此可以作为一个通用的,可访问的前提而不是我的实际域模型。)

One possible way would be if your actual data tables were stored using a left/right tree (example here: http://www.sitepoint.com/hierarchical-data-database-2/ . Note, that example is in MySQL/PHP, but it's trivial to implement). 一种可能的方法是使用左/右树存储您的实际数据表(例如: http//www.sitepoint.com/hierarchical-data-database-2/ 。注意,该示例在MySQL / PHP中,但实施起来很简单)。 Using this, you can find out all forums that fall within a parent's left/right values and given that, you can retrieve all posts who's forum IDs is IN those forum IDs. 使用此功能,您可以找到属于父级左/右值的所有论坛,并且您可以检索所有论坛ID IN这些论坛ID中的帖子。

I'm sure you might get a few proper answers regarding the Linq queries. 我相信你可能会得到一些关于Linq查询的正确答案。 I'm posting this as an advisory when it comes to the SQL side of things. 当谈到SQL方面时,我发布这个作为建议。

I had a similar issue with a virtual filesystem in SQL. 我在SQL中遇到了与虚拟文件系统类似的问题。 I needed to be able to query files in folders recursively - with folders, of course, having a recursive parent-child relationship. 我需要能够递归地查询文件夹中的文件 - 当然,文件夹具有递归的父子关系。 I also needed it to be fast, and I certainly didn't want to be dropping back to client-side processing. 我还需要快速,我当然不希望退回到客户端处理。

For performance I ended up writing stored procedures and inline functions - unfortunately much too complicated to post here (and I might get the sack for sharing company code!). 为了性能,我最终编写了存储过程和内联函数 - 遗憾的是,这里发布的内容太复杂了(我可能会因为共享公司代码而烦恼!)。 The key, however, was to learn how to work with Recursive CTEs http://msdn.microsoft.com/en-us/library/ms186243.aspx . 然而,关键是学习如何使用递归CTE http://msdn.microsoft.com/en-us/library/ms186243.aspx It took me a few days to nail it but the performance is incredible (they are very easy to get wrong though - so pay attention to the query plans). 我花了几天时间来指出它,但性能令人难以置信(尽管它们很容易出错 - 所以要注意查询计划)。

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

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