我有 sql 服务器分层数据 table 以及此样本结构和数据:

I have sql server hierarchical data table with this sample structure and data:

我的table结构是:

Id      name     size     type      parrent_Id

1       AAAA     2k        t1       null
2       BB       2k        t2       1
3       CC       1k        t3       1
4       DDDD     2k        t4       null
5       EE       2k        t5       4
6       FF       1k        t6       5 

我需要一个从 table 生成 JSON 结构的 SQL 查询,以便在 primeng 树 table 组件中使用它。它需要 JSON 这样的结构。我正在使用 asp.net 核心网络 api 和 sql 服务器:

      {
            "data":
            [
                {
                    "data":{
                        "name":"Documents",
                        "size":"2k",
                        "type":"Folder"
                    },
                    "children":[
                        {
                            "data":{
                                "name":"Work",
                                "size":"5k",
                                "type":"Folder"
                         },
                    ]
                }
             ]

假设您正在使用 EF Core 并且您的模型看起来像:

public class XModel {
    public int Id {get;set;}
    public string Name {get;set;}
    public string Size {get;set;}
    public string Type {get;set;}

    public int? ParentId {get;set;}
    public XModel Parent {get;set;}

    public IList<XModel> Children {get;set;}
}

因为您需要一个带有 data & children 字段的模型。让我们为他们创建 DTO 模型:

public class Data {
    public int Id {get;set;}
    public string Name {get;set;}
    public string Size {get;set;}
    public string Type {get;set;}

    [JsonIgnore]
    public int? ParentId {get;set;}
}

public class Dto {
    public Data Data {get;set;}
    public IList<Dto> Children{get;set;}
}

让我们构建一个构建树的扩展方法

public static class TreeLikeExtensions
{
    public static IList<Dto> BuildTrees(this IQueryable<XModel> models)
    {
        var dtos = models.Select(m =>new Dto{
            Data = new Data { Id = m.Id, Name = m.Name, Size =m.Size, Type = m.Type, ParentId = m.ParentId, },
            Children = null,
        }).ToList();
        return BuildTrees(null, dtos);
    }

    // private helper function that builds tree recursively
    private static IList<Dto> BuildTrees(int? pid, IList<Dto> candicates)
    {
        var children = candicates.Where(c => c.Data.ParentId == pid).ToList();
        if (children==null || children.Count() == 0){
            return null; 
        }
        foreach (var i in children){
            i.Children= BuildTrees(i.Data.Id, candicates);
        }
        return children;
    }
}

要获取树,只需调用 BuildTrees():

var result = _context.XModel.BuildTrees();

要在序列化时忽略 null 个子 属性,只需添加如下设置:

// var settings= new JsonSerializerSettings{
//     NullValueHandling = NullValueHandling.Ignore,
//     ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
// }

或在 Startup.cs 中配置 MVC 服务:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .AddJsonOptions(o =>{
        o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    });

一个工作演示