开发者

Linq return list of categories in order

开发者 https://www.devze.com 2023-03-26 09:00 出处:网络
I tried to search for this on Google/SO but I didn\'t really know what to search for! Given: tblCategories

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

精彩评论

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

关注公众号