递归实现jsonTree
阅读原文时间:2023年07月09日阅读:1

using System;
using System.Collections.Generic;
using System.Text;
using WeChatApi.Model;
using System.Linq;
namespace Help
{
public static class TreeGridHelper
{
//递归获取所有树结构的数据
public static IList GetData(this List data)
{
List nodes = data.Where(x => x.parentId == "0").Select(x => new TreeGridModel { id = x.id, parentId = x.parentId, name = x.name,code = x.code,orderNumber = x.orderNumber ,remark = x.remark}).ToList();
foreach (TreeGridModel item in nodes)
{
item.children = GetChildrens(item, data);
}
return nodes;
}
//递归获取子节点
public static IList GetChildrens(TreeGridModel data, List dataList)
{
IList childrens = dataList.Where(c => c.parentId == data.id).Select(x => new TreeGridModel { id = x.id, parentId = x.parentId, name = x.name, code = x.code, orderNumber = x.orderNumber, remark = x.remark }).ToList();
foreach (TreeGridModel item in childrens)
{
item.children = GetChildrens(item, dataList);
}
return childrens;
}
}
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章