开发者

Linq return list of categories in order

I tried to search for this on Google/SO but I didn't really know what to search for!

Given:

tblCategories
-----------------
ID
Name
ParentID

And given say a category ID n how would we return it's parent开发者_Python百科 category, and the parent of it's parent etc, until ParentID = 0?


There are plenty of other questions on this type of issue - try searching for "recursive linq query" - https://stackoverflow.com/search?q=recursive+linq+query

The general answers are:

  • either fetch everything into memory and use something like AsHierarchy in Linq-to-Objects - see http://www.scip.be/index.php?Page=ArticlesNET18#AsHierarchy
  • or write a CommonTableExpression in a stored procedure to help - http://msdn.microsoft.com/en-us/library/ms186243.aspx
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜