I have a self-referencing table with an Id, CategoryName, and ParentId. It's a typical scenario of a hierarchy table of categories that can themselves be divided into categories that DB experts tell me is called the adjacency model.
What I want is to use Linq to SQL to query for subcategories that themselves are related to no other subcategories, ie they are immediate leaf nodes of some given category or subcategory.
The easy part, I got, which is just getting the subcategories. Almost embarrassed to put the code here. But we do like to see code..
IList<Categories> subcategories = context.Where( c => c.ParentId == 1).ToList();
But narrowing it to categories with no subcategories is turning me around. Any help would be much appreciated.
Thanks for you help. Jeff
UPDATE** It would appear this works, but if someone could confirm that it is "proper" I'd be grateful. So, if I want leaf nodes under a category with Id = 1, I would do this:
Categories.Where( c => !c.Children.Any ( d => d.ParentId == c.Id)).Where( e => e.ParentId == 1)
"Children" is the name Linq gives the self-referencing association.