LINQ to SQL的递归查询 [英] linq to sql recursive query

查看:226
本文介绍了LINQ to SQL的递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EmployeeId  Name  ManagerId
------------------------------
1           A     null
2           B     null
3           C     1
4           D     3
5           E     2

只是用这个表,怎么能(使用LINQ to SQL)LINQ查询被写入递归获取父数据。

just using this table, how can a linq query (using linq to sql) be written to fetch the parent data recursively.

例如,如果ID选择的雇主是4,应该给员工的名单与ID:4,3,1

For instance if the Employer Id selected is 4 it should give list of employees with Id: 4, 3, 1

感谢。

推荐答案

这.AsHierarchy()扩展方法可能是有用的:的链接。然而,这只是提供一个简单的方法把你的成果转化为链接对象的作品。为了做到这一点,它会只得到所有的记录,并运行自己的本地递归查询。

This .AsHierarchy() extension method may be useful: link. However, this only works by providing an easy way to throw your results into linked objects. In order to do that, it'll just get all the records and run its own local recursive query.

如果你正在寻找一个LINQ查询,将直接转化为通过LINQ递归SQL查询,SQL,你不会找到它。为了获得最佳性能,CTE在存储过程中也可能是你在找什么。如果你有一个非常简单的页面,需要反正加载整个树时,AsHierarchy方法可能会满足您的需求。

If you're looking for a LINQ query that will directly translate to a recursive SQL query via LINQ to SQL, you won't find it. For the best performance, a CTE in a stored procedure is probably what you're looking for. If you have a really simple page that needs to load the whole tree anyway, the AsHierarchy method would probably fit your needs.

这篇关于LINQ to SQL的递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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