的扁平化分层数据转换为结构化的

的扁平化分层数据转换为结构化的

本文介绍了使用 C#/Linq 将来自 SQL Server 的扁平化分层数据转换为结构化的 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 MVC 应用程序,它从结构如下的 SQL Server 表中检索数据:

I am developing an MVC app that retrieves data from a table in SQL Server that is structured like so:

+-----------------------------------+
| Id | Name   | Hierarchy   | Depth |
|-----------------------------------|
| 01 | Justin | /           |     0 |
| 02 | Chris  | /1          |     1 |
| 03 | Beth   | /1/1        |     2 |
+-----------------------------------+

Hierarchy 列中的示例数据是hierarchyid 数据类型的字符串表示,Depth 列使用hierarchyid::GetLevel() 方法.

The example data in the Hierarchy column is the string representation of the hierarchyid datatype, and the Depth column is computed using the hierarchyid::GetLevel() method.

使用实体框架 4.1,我已将上表映射到此类:

Using Entity Framework 4.1, I have mapped the above table to this class:

public class Node {
    public int Id { get; set; }
    public string Name { get; set; }
    public string HierarchyPath { get; set; } // String representation of the hierarchyid
    public int Depth { get; set; }
}

我想使用此信息向使用 JS 可视化工具包的用户显示层次结构的图形表示,这需要对数据进行结构化:

I want to use this information to display a graphical representation of the hierarchy to the user using the JS Visualizations Toolkit, which requires the data to be structured:

var node = {
    id: 1,
    name: 'Justin'
    children: [{
        id: 2,
        name: 'Chris',
        children: [{
            id: 3,
            name: 'Beth',
            children: []
        }]
    }]
}

我在开发将我的模型列表转换为结构化 JSON 对象的逻辑时遇到问题.有什么建议吗?

I'm having trouble developing the logic to convert a list of my models into a structured JSON object. Any suggestions?

推荐答案

我现在没有时间修复下面的答案,但鉴于问题中的额外信息,我怀疑您想要保留一个 Dictionary 而不是 List 以便您不依赖任何排序...

I don't have time to fix the answer below right now, but given the extra information in the question, I suspect you want to keep a Dictionary<int, HierarchicalNode> rather than a List<HierarchicalNode> so that you're not relying on any ordering...

我会忘记一开始的 JSON 表示,而是专注于构建层次结构的内存 POCO 表示.要做到这一点,我会使用这样的东西:

I would forget about the JSON representation to start with, and concentrate on building an in-memory POCO representation of the hierarchy. To do that, I'd use something like this:

class HierarchicalNode
{
    private readonly List<HierarchicalNode> children =
        new List<HierarchicalNode>();
    public List<HierarchicalNode> Children { get { return children; } }

    private readonly string name;
    public string Name { get { return name; } }

    private readonly int id;
    public int Id { get { return id; } }

    public HierarchicalNode(string name, int id)
    {
        this.name = name;
        this.id = id;
    }
}

然后像这样构建树:

// Make sure we get everything in a sensible order, parents before children
var query = context.Nodes.OrderBy(x => x.Depth);

var root = new HierarchicalNode("Root", 0);
foreach (var node in query)
{
    var current = root;
    foreach (string part = node.HierarchyPath.Split(new[] {'/'},
                                   StringSplitOptions.RemoveEmptyEntries))
    {
        int parsedPart = int.Parse(part);
        current = current.Children[parsedPart - 1];
    }
    current.Children.Add(new HierarchicalNode(node.Name, node.Id));
}

这篇关于使用 C#/Linq 将来自 SQL Server 的扁平化分层数据转换为结构化的 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:44