LINQ2SQL:orderby note.hasChildren(),名称升序

发布于 2024-09-03 05:10:20 字数 927 浏览 8 评论 0原文

我有一个分层数据结构,我在网页中将其显示为树视图。

我希望对数据进行排序,以首先显示按字母顺序排序的没有子节点的节点, then 在这些按字母顺序排列且有子节点的节点下。 当前我正在对一组中的所有节点进行排序,这意味着具有子节点的节点出现在没有子节点的节点旁边。

我正在使用递归方法来构建树视图,该树视图的核心是以下 LINQ 代码:

    var filteredCategory = from c in category
                           orderby c.Name ascending
                           where c.ParentCategoryId == parentCategoryId && c.Active == true
                           select c;

这就是我想要增强的 orderby 语句。

数据库表结构如下图所示:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2024-09-10 05:10:20
var filteredCategory = category
  .Where(c => c.ParentCategoryId == parentCategoryId
    && c.Active == true)
  .OrderBy(c => c.Children.Any() ? 1 : 2)
  .ThenBy(c => c.Name);

如果没有 Children 属性,请转到 linq to sql 设计器并创建一个添加该属性的关联。

var filteredCategory = category
  .Where(c => c.ParentCategoryId == parentCategoryId
    && c.Active == true)
  .OrderBy(c => c.Children.Any() ? 1 : 2)
  .ThenBy(c => c.Name);

If you don't have a Children property, go to the linq to sql designer and create an association that adds the property.

女中豪杰 2024-09-10 05:10:20

尝试使用子 IQueryable<> 来执行此操作(替换 c.Category):

var filteredCategory = from c in category 
       orderby c.Category.Count == 0
       where c.ParentCategoryId == parentCategoryId && c.Active == true 
       select c; 

Try this (replace c.Category) with the child IQueryable<>:

var filteredCategory = from c in category 
       orderby c.Category.Count == 0
       where c.ParentCategoryId == parentCategoryId && c.Active == true 
       select c; 
清晰传感 2024-09-10 05:10:20

如果您使用的是 Linq2SQL,难道您不能通过获取所有没有父级的类别,然后根据需要访问子级来构建树吗?

我的意思是像这样的查询来选择根类别:

Category[] rootCats = (from c in category
    orderby c.Name ascending
    where c.ParentCategoryId == 0 // that is, no parent = root element
        && c.Active == true
    select c).ToArray();

然后通过 oneCat.Category.Where( cat => cat.活动 == true)

If you're using Linq2SQL, can't you build the tree by just getting all the Categories without parent, and then access the children as needed to build the tree?

I mean something like this query to select the root Categories:

Category[] rootCats = (from c in category
    orderby c.Name ascending
    where c.ParentCategoryId == 0 // that is, no parent = root element
        && c.Active == true
    select c).ToArray();

And then accessing the children of a given Category (IEnumerable<Category>) via oneCat.Category.Where( cat => cat.Active == true).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文