我有以下数据想要转换为树状结构。
所以我需要将其变成4级深的树结构。基本上,这种结构的结构是,如果该图层的索引列为空,则该项目为单个元素。如果索引列不为null,则该层为数组索引。唯一的例外是最后一列“ Last Variable Array Length”,它是链中最后一个变量的数组元素数。
+---------------+---------------+--------------+---------------+--------------+---------------+--------------------------+----------------------------+
| Layer 1 Name | Layer 1 Index | Layer 2 Name | Layer 2 Index | Layer 3 Name | Layer 3 Index | Last Layer Variable Name | Last Variable Array Length |
+---------------+---------------+--------------+---------------+--------------+---------------+--------------------------+----------------------------+
| RecipeChem | | | | | | | 49 |
| RecipeDes | | | | | | | 49 |
| RecipeWT | | | | | | | 49 |
| SpecGrav | | | | | | | 29 |
| DS1_QueString | | | | | | | 10 |
| Tower | 0 | | | | | RecipeName | 60 |
| Tower | 0 | Boost | | | | ChemSel | 49 |
| Tower | 0 | | | | | InputOverride | 31 |
| Tower | 0 | | | | | PreActSPValue | 14 |
| Tower | 0 | Boost | | | | TimeIn | 12 |
| Tower | 0 | | | | | DestName | 11 |
| Tower | 0 | | | | | ChemName | 9 |
| Tower | 0 | Boost | | | | AckStep | |
| Tower | 0 | Boost | | | | Alarm | |
| Tower | 0 | Boost | | | | CamStartPB | |
| Tower | 0 | Boost | | Selected | | Time | |
| Tower | 0 | Boost | | Selected | | WeightL | |
| Tower | 0 | Boost | | Selected | | WeightmL | |
| Tower | 0 | Boost | | Selected | | WeightmOz | |
| Tower | 1 | | | | | JugA_RecipeName | 60 |
| Tower | 1 | Boost | | | | BoostA_ChemSel | 49 |
| Tower | 1 | | | | | JugA_InputOverride | 31 |
| Tower | 1 | | | | | JugA_PreActSPValue | 14 |
| Tower | 1 | Boost | | | | BoostA_TimeIn | 12 |
| Tower | 1 | | | | | JugA_DestName | 11 |
| Tower | 1 | | | | | JugA_ChemName | 9 |
| Tower | 1 | Boost | | | | BoostA.AckStep | |
| Tower | 1 | Boost | | | | BoostA.Alarm | |
| Tower | 1 | Boost | | | | BoostA.CamStartPB | |
| Tower | 1 | Boost | | Selected | | BoostATime | |
| Tower | 1 | Boost | | Selected | | BoostAWeightL | |
| Tower | 1 | Boost | | Selected | | BoostAWeightmL | |
| Tower | 1 | Boost | | Selected | | BoostAWeightmOz | |
+---------------+---------------+--------------+---------------+--------------+---------------+--------------------------+----------------------------+
到目前为止,这是我的代码。我现在只是陷入分析瘫痪。
public static void MapDataStructureToTree(List<VariableStructureTableRow> rows)
{
List<IMember> Members = new List<IMember>();
var stuff = rows.GroupBy(x => new { x.Layer1, x.Layer1Index, x.Layer2, x.Layer2Index, x.Layer3, x.Layer3Index });
var currentLayer0 = new Member();
var currentLayer1 = new Member();
var currentLayer2 = new Member();
var currentLayer3 = new Member();
foreach (var group in stuff)
{
if (group.Key.Layer3Index != null) { }
else if (group.Key.Layer3 != null) { }
else if (group.Key.Layer2Index != null) { }
else if (group.Key.Layer2 != null) { }
else if (group.Key.Layer1Index != null) { }
else if (group.Key.Layer1 != null) { }
else if (group.Key.Layer1 is null)
{
foreach (var item in group)
{
Members.Add(new Member() { Name = item.VariableName });
}
}
}
}
public class VariableStructureTableRow
{
public string VariableName { get; set; }
public int VariableDimension { get; set; }
public string Layer1 { get; set; }
public int? Layer1Index { get; set; }
public string Layer2 { get; set; }
public int? Layer2Index { get; set; }
public string Layer3 { get; set; }
public int? Layer3Index { get; set; }
}
interface IMember
{
string Name { get; set; }
List<IMember> Children { get; set; }
IMember Parent { get; set; }
}
class Member : IMember
{
public string Name { get; set; }
public List<IMember> Children { get; set; }
public IMember Parent { get; set; }
}
class ArrayMember : Member
{
//** is Array
}
class ArrayElement : Member
{
public int ArrayIndex { get; set; }
}