LINQ to SQL自引用表? [英] LINQ to SQL for self-referencing tables?

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

问题描述

我有一个自引用Categories表。每个类别具有类别ID,父类别ID,类别名称等。并且每个类别可以具有任何数量的子类别,并且这些子类别中的每一个可以具有任何数量的子类别,等等。所以基本上树可以是X级深。

I have a self referencing Categories table. Each Category has a CategoryID, ParentCategoryID, CategoryName, etc. And each category can have any number of sub categories, and each of those sub categories can have any number of sub categories, and so and and so forth. So basically the tree can be X levels deep.

然后产品关联到叶(子)类别。有没有办法使用LINQ to SQL获得任何给定类别的所有产品(这将是所有与其叶子后代相关联的产品)?

Then Products are associated to leaf (sub) Categories. Is there a way to get all the Products for any given Category (which would be all the products associated to all its leaf descendants) using LINQ to SQL?

这就像是一个递归问题。是不是更好地使用存储过程?

This feels like a recursive problem. Is it better to used a Stored Procedure instead?

推荐答案

我不认为linq-to-sql有一个很好的答案这个问题。因为你使用sql server 2005,你可以使用CTE来做层次查询。存储过程或内联查询(使用DataContext.ExecuteQuery)将会做到这一点。

I don't think linq-to-sql has a good answer to this problem. Since you are using sql server 2005 you can use CTEs to do hierarchical queries. Either a stored procedure or an inline query (using DataContext.ExecuteQuery) will do the trick.

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

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