LINQ Query to Produce Non-Empty Nested Menu?

Create classes to hold your information like so public class BusinessCat { public string Name{get;set;} public string Caption{get;set;} public List Children{get;set;}; } var results = from c in BizCategories where c. ParentID == null select new BusinessCat{ Name = c. Name, Caption = c.

Caption, Children = (from d in BizCategories where d. ParentID == c. BizCatID && d.Bizs.Count() > 0 select d).ToList() } and with this you have a list full of Children and you can simply iterate through it with a foreach You can also do .Join() extension method for where d.

ParentID == c. BizCatID && d.Bizs.Count().

Create classes to hold your information like so public class BusinessCat { public string Name{get;set;} public string Caption{get;set;} public List Children{get;set;}; } var results = from c in BizCategories where c. ParentID == null select new BusinessCat{ Name = c. Name, Caption = c.

Caption, Children = (from d in BizCategories where d. ParentID == c. BizCatID && d.Bizs.Count() > 0 select d).ToList() } and with this you have a list full of Children and you can simply iterate through it with a foreach.

You can also do .Join() extension method for where d. ParentID == c. BizCatID && d.Bizs.Count().

It already is a class, from LINQ to Sql. I suppose I could add the Children property through a partial class addition. Your solution looks practical, I'll report on how it works out for me.

Thank you! – Chad Mar 13 '09 at 15:58 After giving it a go, you're right. I was WAAAAY over-thinking things.So simple, should have seen it sooner.

Thank you! – Chad Mar 18 '09 at 4:46.

I'm not sure about the recursion bit but this might get you started. Is a two-level query. I'm guessing you want to support any number of levels.

Var allcats = (from c in db. BizCategories select c).ToList(); // This will retrieve them all from the database. Var twoLevels = from c in allcats where c.

ParentID == null select new { Name = c. Name, Caption = c. Caption, Children = from d in allcats where d.

ParentID == c. BizCatID select d }.

That's got me thinking... off to fireup LINQPad and run some queries... Thanks! – Chad Mar 13 '09 at 7:01.

A query like this is getting close to what I need: from c in BizCategories where c. ParentID == null select new { Name = c. Name, Caption = c.

Caption, Children = from d in BizCategories where d. ParentID == c. BizCatID && d.Bizs.Count() > 0 select d } I should be able run another LINQ query over these results and pull out exactly what I need.

Not at a computer where I can test that now, but will reply once I can. Thanks!

To see my problem in action, visit www.apoads.com and hover over the "Local Businesses" menu item. It's a series of nested unordered lists generated from a db call. Go ahead and click on a few of the items underneath "Local Businesses", you'll see that most of the categories are empty (the site is very new).

I only want to show the categories that actually contain a local business. That obviously won't work, cause I'll need the parent category to show if the child category contains a business.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions