I have the following table
PNLId PNLCode PNLParentId Operator Sign
0 0 ~ S
49 C 51 + NULL
50 Z 51 + NULL
51 Y 107 / NULL
52 B 107 / NULL
53 B 108 + NULL
I define the following class
class Node
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Operator { get; set; }
public string Sign { 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));
return "Node: " + Operator + " " + Id + " "
+ string.Join(",", Children.Select(x => string.Format("({0}, {1})", x.Sign, x.Id)));
}
}
var map = new Dictionary<int, Node>();
var rootNodes = new List<Node>();
foreach (DataRow row in dt.Rows)
{
int id = Convert.ToInt32(row["PNLId"]);
int? parentId = null;
if (!row.IsNull("PNLParentId"))
{
parentId = Convert.ToInt32(row["PNLParentId"]);
}
string op = Convert.ToString(row["Operator"]);
string sign = Convert.ToString(row["Sign"]);
map[id] = new Node
{
Id = id,
ParentId = parentId,
Operator = op,
Sign=sign
};
}
foreach (var pair in map)
{
if (pair.Value.ParentId.HasValue)
{
var parent = map[pair.Value.ParentId.Value];
pair.Value.Parent = parent;
parent.Children.Add(pair.Value);
parent.Operator = pair.Value.Operator;
}
else
{
rootNodes.Add(pair.Value);
}
}
For the PNLParentId 107
[107 Node: + 107 (, 51),(, 52)]
How can I modify the code above to get the right operator for 107 which is /
?