<p>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.</p>

<p>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? </p>

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

[{'answer_id': 43121, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 2, 'selected': False, 'text': "<p>Well here is a terrible rushed implementation using LINQ.\nDon't use this :-)</p>\n\n<pre><code>public IQueryable GetCategories(Category parent)\n{\n    var cats = (parent.Categories);\n    foreach (Category c in cats )\n    {\n        cats  = cats .Concat(GetCategories(c));\n    }\n    return a;\n}\n</code></pre>\n"}, {'answer_id': 43170, 'author': 'Sander', 'author_id': 2928, 'author_profile': 'https://Stackoverflow.com/users/2928', 'pm_score': 1, 'selected': False, 'text': '<p>The performant approach is to create an insert/modify/delete trigger which maintains an entirely different table which contains node-ancestor pairs for all ancestors of all nodes. This way, the lookup is O(N).</p>\n\n<p>To use it for getting all products belonging to a node and all of its descendants, you can just select all category nodes which have your target node as an ancestor. After this, you simply select any products belonging to any of these categories.</p>\n'}, {'answer_id': 43254, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 3, 'selected': True, 'text': "<p>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. </p>\n"}, {'answer_id': 413555, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>The way I handle this is by using some extension methods (filters).  I've written up some sample code from a project I have implemented this on. Look specifically at the lines where I'm populating a ParentPartner object and a SubPartners List.</p>\n\n<pre><code>public IQueryable&lt;Partner&gt; GetPartners()\n        {\n            return from p in db.Partners\n                   select new Partner\n                   {\n                       PartnerId = p.PartnerId,\n                       CompanyName = p.CompanyName,\n                       Address1 = p.Address1,\n                       Address2 = p.Address2,\n                       Website = p.Website,\n                       City = p.City,\n                       State = p.State,\n                       County = p.County,\n                       Country = p.Country,\n                       Zip = p.Zip,\n                       ParentPartner = GetPartners().WithPartnerId(p.ParentPartnerId).ToList().SingleOrDefault(),\n                       SubPartners = GetPartners().WithParentPartnerId(p.PartnerId).ToList()\n                   };\n        }\n\n\npublic static IQueryable&lt;Partner&gt; WithPartnerId(this IQueryable&lt;Partner&gt; qry, int? partnerId)\n        {\n            return from t in qry\n                   where t.PartnerId == partnerId\n                   select t;\n        }\n\npublic static IQueryable&lt;Partner&gt; WithParentPartnerId(this IQueryable&lt;Partner&gt; qry, int? parentPartnerId)\n        {\n            return from p in qry\n                   where p.ParentPartner.PartnerId == parentPartnerId\n                   select p;\n        }\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/43111', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/653/']