我有下表
PNLId PNLParentId Operator Sign DisplayingOrder PNLGrpId
0 0 ~ 1 1 1
1 105 + 1 1 2
11 20 + -1 1 5
我使用类Node
来获取表格中的父/子
class Node
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Operator { get; set; }
public Node Parent { get; set; }
public IList<Node> Children { get; set; }
public Node()
{
Children = new List<Node>();
}
public override string ToString()
{
return "Node: " + Operator + " " + Id + " "
+ string.Join(",", Children.Select(x => x.Id));
}
}
我得到以下结果
[50 Node: + 50 36,37]
[51 Node: + 51 49,50]
[52 Node: + 52 20,21,22]
[69 Node: + 69 56,57,58,59]
如何修改类节点以使子节点成对(Sign,child),类似
[50 Node: + 50 (-1,36),(1,37)]
[51 Node: + 51 (-1,49),(-1,50)]
[52 Node: + 52 (1,20),(-1,21),(-1,22)]
[69 Node: + 69 (-1,56),(-1,57),(1,58),(1,59)]
答案 0 :(得分:2)
实施ToString
,如下所示:
return "Node: " + Operator + " " + Id + " " +
string.Join(",", Children.Select
(x => string.Format("({0},{1})", x.Sign, x.Id)));
答案 1 :(得分:1)
出于确切目的,请考虑以下步骤:
第1步 - 将Sign
属性添加到您的班级
class Node
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Operator { get; set; }
public Node Parent { get; set; }
public IList<Node> Children { get; set; }
// HERE WE GO
public int Sign { get; set; }
...
}
步骤2 - 确定您有权使用的表与#34之间的连接;以下是&#34;和Node
类。在您的代码中确切地说明Id
属性是PNLId
列的.NET表现形式?
第3步 - 一旦回答了第2步的问题,您必须为Sign
属性Sign
列关系重新创建相同的效果
第4步 - 改变ToString
方法,如下所示:
class Node
{
...
public override string ToString()
{
return "Node: " + Operator + " " + Id + " "
+ string.Join(",", Children.Select(x => string.Format("({0}, {1})", x.Sign, x.Id)));
}
}
注意:检查Children.Select
lambda参数的修改:
Children.Select(x => string.Format("({0}, {1})", x.Sign, x.Id))