LINQ2SQL: orderby note.hasChildren(), name ascending
- by Peter Bridger
I have a hierarchical data structure which I'm displaying in a webpage as a treeview.
I want to data to be ordered to first show nodes ordered alphabetically which have no children, then under these nodes ordered alphabetically which have children. Currently I'm ordering all nodes in one group, which means nodes with children appear next to nodes with no children.
I'm using a recursive method to build up the treeview, which has this LINQ code at it's heart:
var filteredCategory = from c in category
orderby c.Name ascending
where c.ParentCategoryId == parentCategoryId && c.Active == true
select c;
So this is the orderby statement I want to enhance.
Shown below is the database table structure:
[dbo].[Category](
[CategoryId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NOT NULL,
[Level] [tinyint] NOT NULL,
[ParentCategoryId] [int] NOT NULL,
[Selectable] [bit] NOT NULL CONSTRAINT [DF_Category_Selectable] DEFAULT ((1)),
[Active] [bit] NOT NULL CONSTRAINT [DF_Category_Active] DEFAULT ((1))