LINQ2SQL: orderby note.hasChildren(), name ascending

Posted by Peter Bridger on Stack Overflow See other posts from Stack Overflow or by Peter Bridger
Published on 2010-05-25T09:11:48Z Indexed on 2010/05/25 10:21 UTC
Read the original article Hit count: 267

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))

© Stack Overflow or respective owner

Related posts about sql

Related posts about LINQ