Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 /?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

Perhaps the + operator is coming from another child:

49  C   51  +   NULL
50  Z   51  +   NULL

Depending on the order the rows are fetched, the 49 or 50 Node sets the Operator for the parent Node 51 to +.

EDIT

It hinges on this line of your code:

parent.Operator = pair.Value.Operator;

From your post, you assume that the parent is 107 and the pair.Value is 51 or 52. This, when assigning the child to the parent, the / operator is assigned to the 107 parent.

However, when the 49 and 50 children are processed, the + operator is applied to their parent, Node 51. Thus, when applying the operator from Node 51 to parent 107, the + operator from 49 or 50 is applied to 107 via Node 51.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...