层次结构的递归.Include()... [英] Recursive .Include() for hierarchies...

查看:72
本文介绍了层次结构的递归.Include()...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我还没有找到任何关于此的信息。我在我的数据库中有一个存储在论坛表中的分层论坛结构。每个论坛都有一个ParentForumID,因此可以创建父/子关系。我需要使用LINQ to Entities / eSQL高效地加载整个论坛层次结构,我不知道该怎么做。如果我尝试这样的事情:



ObjectQuery < 论坛> forums = context.Forums.Include(" ChildForums" );



当我在运行时迭代时,我只获得前两个级别的子论坛。但是,如果我在该行之后断开,并使用调试器枚举结果,则返回所有级别。我不确定为什么用调试器可视化器枚举它和运行代码strait之间存在差异。当我调用 .Include(" ChildForums")时,我需要我的运行时行为以递归方式返回所有级别。有没有办法做到这一点?



编辑:



如果我将 .Where(f => f.ForumID == 1)添加到该查询中,那么我只会得到ID == 1的论坛的直接孩子。我根本没有得到任何类型的递归。有没有办法在调用.Include时强制递归?我更喜欢批量调用数据库,而不是迭代集合一次来调用.Load,然后再次迭代它以使用或呈现信息。

解决方案

Hey Jon



我不太清楚你的意思是什么。



你是否只获得了childForums一些论坛归还了吗?或者你想获得与儿童论坛相关的其他数据吗?



如果是后者,你有类似



论坛


childforums


孙子论坛(因为没有更好的术语)



那么你可以构建你的包含像这样:



context.Forums.Include(" ChildForums.GrandchildForums")



这有帮助吗?或者你的问题是第一个,对我来说这听起来像是奇怪的行为。



julie


I have not been able to find any information about this by searching the net so far. I have a hierarchical forum structure, stored in a Forums table, in my database. Each Forum has a ParentForumID so parent/child relationships may be created. I need to efficiently load an entire forum hierarchy with LINQ to Entities/eSQL, and I am not sure how to do that. If I try with something like this:

 

ObjectQuery<Forum> forums = context.Forums.Include("ChildForums");

 

When I iterate at runtime, I only get the first two levels of child forums. However, if I break right after that line, and enumerate results with the debugger, all levels are returned. I am unsure why there is a difference between enumerating it with the debugger visualizer, and running the code strait up. I need my runtime behavior to return all levels recursively when I call .Include("ChildForums"). Is there a way to do that?

 

EDIT:

 

If I tack on a .Where(f => f.ForumID == 1) onto that query, then I only get the immediate children of the forum with ID == 1. I do not get any kind of recursion at all. Is there a way to force recursion when calling .Include? I would preferr to batch the call to the database, rather than iterate the collection once to call .Load, then iterate it again to work with or render the information.

解决方案

Hey Jon

 

I don't quite get what you mean by levels.

 

Are you getting childForums for only SOME of the forums returned? Or do you want to get other data that is related to teh ChildForums?

 

If it's the latter and you have something like

 

forums 

  childforums

    grandchildforums (for lack of a better term)

 

then you can construct your include like this:

 

context.Forums.Include("ChildForums.GrandchildForums")

 

Does that help? Or is your problem the first, which to me sounds like strange behavior.

 

julie


这篇关于层次结构的递归.Include()...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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