csharp 父子相关的自引用表数据绑定在一起并相应地排序(使用递归调用).cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 父子相关的自引用表数据绑定在一起并相应地排序(使用递归调用).cs相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
    public class UserPrivilegeViewModel
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public int? MenuId { get; set; }
        public string MenuName { get; set; }
        public List<UserPrivilegeViewModel> Children { get; set; }
    }

    class Program
    {
        static void Main()
        {
            List<UserPrivilegeViewModel> finalList = new List<UserPrivilegeViewModel>();
            List<UserPrivilegeViewModel> categories = new List<UserPrivilegeViewModel>()
            {
                new UserPrivilegeViewModel() {Id = 1, MenuId = 10, MenuName = "Item 1", ParentId = 0},
                new UserPrivilegeViewModel() {Id = 2, MenuId = 20,  MenuName = "Item 2", ParentId = 0},
                new UserPrivilegeViewModel() {Id = 3, MenuId = 30,  MenuName = "Item 3", ParentId = 0},
                new UserPrivilegeViewModel() {Id = 4, MenuId = 40,  MenuName = "Item 1.1", ParentId = 10},
                new UserPrivilegeViewModel() {Id = 5, MenuId = 50,  MenuName = "Item 1.2", ParentId = 10},
                new UserPrivilegeViewModel() {Id = 6, MenuId = 60,  MenuName = "Item 3.1", ParentId = 30},
                new UserPrivilegeViewModel() {Id = 7, MenuId = 70,  MenuName = "Item 3.2", ParentId = 30}
            };

            List<UserPrivilegeViewModel> hierarchy = new List<UserPrivilegeViewModel>();
            hierarchy = categories.Where(c => c.ParentId == 0).Select(c => new UserPrivilegeViewModel()
                                    {
                                        Id = c.Id,
                                        MenuName = c.MenuName,
                                        ParentId = c.ParentId,
                                        Children = GetChildren(categories, c.MenuId)
                                    })
                                    .ToList();

            foreach (var item in hierarchy)
            {
                if (item.Children.Count != 0)
                {
                    finalList.Add(item);
                    foreach (var subItem in item.Children)
                    {
                        finalList.Add(subItem);
                    }
                }
                else
                {
                    finalList.Add(item);
                }
            }


            Console.ReadLine();
        }

        public static List<UserPrivilegeViewModel> GetChildren(List<UserPrivilegeViewModel> list, int? parentId)
        {
            return list.Where(c => c.ParentId == parentId)
                .Select(c => new UserPrivilegeViewModel
                {
                    Id = c.Id,
                    MenuName = c.MenuName,
                    ParentId = c.ParentId,
                    Children = GetChildren(list, c.Id)
                })
                .ToList();
        }
    }
}

以上是关于csharp 父子相关的自引用表数据绑定在一起并相应地排序(使用递归调用).cs的主要内容,如果未能解决你的问题,请参考以下文章

如何在维度模式中建模父子关系表

Vue2.0父子组件之间的双向数据绑定问题解决方案

jquery treeTable插件使用细则

Springboot jpa:实体无法绑定不在表列中的自定义查询中的数据

C#9引入的自定义类型record

如何将数据插入到sql server中的自引用表中? [复制]