[英]Why sometimes 2 objects reference the same but not always
Following the last answer : Recursive method to convert flat collection to hierarchal collection?遵循最后一个答案: 将平面集合转换为分层集合的递归方法?
I want to use the same method CreateTree but with another object than Hierarchy: ItemNode:我想使用相同的方法 CreateTree 但使用另一个对象而不是层次结构:ItemNode:
public class ItemNode
{
public string Id { get; set; }
public Item Item { get; set; }
public ICollection<ItemNode> Children { get; set; }
}
and the definition of Item:以及 Item 的定义:
public class Item
{
public string ID { get; set; }
public string Name { get; set; }
public int Level { get; set; }
public string ParentId { get; set; }
}
And here the CreateTree methods with the ItemNode:这里是带有 ItemNode 的 CreateTree 方法:
static List<ItemNode> CreateTreeItems(IEnumerable<ItemNode> nodes)
{
Dictionary<string,ItemNode> idToNode = nodes.ToDictionary(n => n.Id, n => n);
List<ItemNode> roots = new List<ItemNode>();
ItemNode root = null;
foreach (var n in nodes)
{
if (n.Item.ParentId == null)
{
if (root != null)
{
roots.Add(root);
}
root = n;
continue;
}
ItemNode parent = idToNode[n.Item.ParentId];
//if (!idToNode.TryGetValue(n.Item.ParentId, out parent))
//{
// //Parent doesn't exist, orphaned entry
//}
parent?.Children.Add(n);
// RETURNS FALSE WHEREAS IN THE ORIGINAL METHOD IT RETURNS TRUE
var test = Object.ReferenceEquals(parent, root);
Debug.WriteLine(test);
}
if (root == null)
{
//There was no root element
}
roots.Add(root);
return roots;
}
It does not work because parent and root does not reference the same object (whereas in the original method, it does).它不起作用,因为 parent 和 root 没有引用同一个对象(而在原始方法中,它确实如此)。 I guess it was linked to the fact that I have added an Item property to the ItemNode class.
我想这与我在 ItemNode 类中添加了一个 Item 属性有关。 But I don't know how to fix it.
但我不知道如何修复它。
Thank you !谢谢 !
Why do you expect the root and parent node references to be equal?为什么您希望根节点和父节点引用相等? Every root node might be a parent node but not every parent node is a root node.
每个根节点都可能是父节点,但并非每个父节点都是根节点。
There might be a situations where the references are equal but it very depends on a sort order of the nodes
collection.可能存在引用相等的情况,但这在很大程度上取决于
nodes
集合的排序顺序。 Actually when root node is placed earlier than the 1st level child node you will see the situation where the references are equal.实际上,当根节点放置在第一级子节点之前时,您会看到引用相等的情况。
I suppose your problem is elsewhere, for instance it might not work if the Nodes
collection doesn't have a root node at all.我想你的问题在别处,例如,如果
Nodes
集合根本没有根节点,它可能无法工作。
Here's the example, try it here https://dotnetfiddle.net/4r52xP这是示例,请在此处尝试https://dotnetfiddle.net/4r52xP
using System;
using System.Collections.Generic;
using System.Linq;
public class Hierarchy
{
public Hierarchy(string iD, string name, int level, string parentID, string topParent)
{
ID = iD;
Name = name;
Level = level;
ParentID = parentID;
Children = new HashSet<Hierarchy>();
}
public string ID { get; set; }
public string Name{ get; set; }
public int Level { get; set; }
public string ParentID { get; set; }
public ICollection<Hierarchy> Children { get; set; }
}
public class Program
{
static Hierarchy CreateTree(IEnumerable<Hierarchy> Nodes)
{
var idToNode = Nodes.ToDictionary(n => n.ID, n => n);
Hierarchy root = null;
foreach (var n in Nodes)
{
if (n.ParentID == null)
{
if (root != null)
{
//there are multiple roots in the data
}
root = n;
continue;
}
Hierarchy parent = null;
if (!idToNode.TryGetValue(n.ParentID, out parent))
{
//Parent doesn't exist, orphaned entry
}
parent.Children.Add(n);
Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(parent, root));
}
if (root == null)
{
//There was no root element
}
return root;
}
public static void Main()
{
Console.WriteLine("Test #1");
List<Hierarchy> l = new List<Hierarchy>();
l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the top of the list
l.Add(new Hierarchy("12345","child1",2,"295152", null));
l.Add(new Hierarchy("54321","child2",2,"295152", null));
l.Add(new Hierarchy("44444","child1a",3,"12345", null));
l.Add(new Hierarchy("33333","child1b",3,"12345", null));
l.Add(new Hierarchy("22222","child2a",3,"54321", null));
l.Add(new Hierarchy("22221","child2b",3,"54321", null));
l.Add(new Hierarchy("22002","child2c",3,"54321", null));
l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
l.Add(new Hierarchy("20101","child2b2",4,"22222", null));
CreateTree(l);
Console.WriteLine("\nTest #2");
l = new List<Hierarchy>();
l.Add(new Hierarchy("12345","child1",2,"295152", null));
l.Add(new Hierarchy("54321","child2",2,"295152", null));
l.Add(new Hierarchy("44444","child1a",3,"12345", null));
l.Add(new Hierarchy("33333","child1b",3,"12345", null));
l.Add(new Hierarchy("22222","child2a",3,"54321", null));
l.Add(new Hierarchy("22221","child2b",3,"54321", null));
l.Add(new Hierarchy("22002","child2c",3,"54321", null));
l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
l.Add(new Hierarchy("20101","child2b2",4,"22222", null));
l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the bottom of the list
CreateTree(l);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.