使用LINQ数据库中检索的树形结构 [英] Retrieving a tree structure from a database using LINQ

查看:282
本文介绍了使用LINQ数据库中检索的树形结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储在数据库中的组织结构图的树结构。
是是一样的东西。

I have an organization chart tree structure stored in a database. Is is something like

ID (int);
Name (String);
ParentID (int)

在C#它是由一类像

class Employee
{
int ID, 
string Name, 
IList < Employee> Subs
} 



我想知道是如何从数据库中检索这些值的最佳方式填满使用LINQ的C#对象(我使用实体框架)

I am wondering how is the best way to retrieve these values from the database to fill up the C# Objects using LINQ (I am using Entity Framework)

有一定的东西比拨打电话,以获得最高的水平,那么重复调用,以获得更好潜艇等。

There must be something better than making a call to get the top level then making repeated calls to get subs and so on.

如何最好地做到这一点?

How best to do it?

推荐答案

我要一个字段添加到实体包括母公司的ID,那么我会拉动整个表装入内存离开列表潜艇空。然后通过标识的对象迭代,并使用LINQ to对象填充列表。只有一个数据库查询,所以应该是合理的。

I'd add a field to the entity to include the parent ID, then I'd pull the whole table into memory leaving the List subs null. Id then iterate through the objects and populate the list using linq to objects. Only one DB query so should be reasonable.

这篇关于使用LINQ数据库中检索的树形结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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