为什么语句不比较?列出项目,重新测量
本文关键字:新测量 测量 项目 语句 比较 为什么 | 更新日期: 2023-09-27 18:29:48
我想添加只有NodeDir 真值的项
public List<Node> BuildTreeHierarchy(List<Node> node, int? pKey)
{
if (node.Where(n => n.NodeDir.Equals(true)).Count() > 0)
{
return node.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Left = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
}
结果:
bear错误
猴子真
wolf True
鸡肉错误
鹳错误
现在在其他部分,我得到了null exeption
*例如,我得到所有带有false但带有true的项目,没有*
public List<Node> BuildTreeHierarchy(List<Node> node, int? pKey)
{
var nodesWithNodeDir = node.Where(n => n.NodeDir.Equals(false));
if (nodesWithNodeDir.Count() > 0)
{
return nodesWithNodeDir.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Left = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
else
{
return nodesWithNodeDir.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Right = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
这是我的逻辑,我需要从数据库中填充左节点和右节点
Storage data = new Storage();
public List<Node> nodes = new List<Node>();
public List<Node> AnimalTree = new List<Node>();
public List<Node> CreateTree()
{
foreach (DataRow animal in data.GetAnimals("select * from AnimalTbl").Rows)
{
Node newNode = new Node();
newNode.ID = Convert.ToInt32(animal["Id"]);
newNode.Name = animal["Name"].ToString();
newNode.Feature = animal["Feature"].ToString();
// newNode.NodeDir = animal["NodeDir"] == DBNull.Value ? (bool?)null : Convert.ToBoolean(animal["NodeDir"]);
newNode.NodeDir = Convert.ToBoolean(animal["NodeDir"]);
newNode.ParentKey = animal["ParentKey"] == DBNull.Value ? (int?)null : Convert.ToInt32(animal["ParentKey"]);
nodes.Add(newNode);
}
AnimalTree = BuildTreeHierarchy(nodes, 1);
return AnimalTree;
}
public List<Node> BuildTreeHierarchy(List<Node> node, int? pKey)
{
List<Node> aa = new List<Node>();
var nodesWithNodeDir = node.Where(n => n.NodeDir.Equals(true));
if (nodesWithNodeDir.Count() > 0)
{
return nodesWithNodeDir.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Left = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
nodesWithNodeDir = node.Where(n => n.NodeDir.Equals(false));
if (nodesWithNodeDir.Count() > 0)
{
return nodesWithNodeDir.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Right = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
else
{
return new List<Node>();
}
}
}
在这里的按钮点击事件中,我需要打印右侧的节点列表,在那里我可以获得null exection
protected void Button2_Click(object sender, EventArgs e)
{
cur++;
PrintTree(nd, cur);
}
private void PrintTree(IEnumerable<Node> nodes,int Current)
{
foreach (var root in nodes)
{
Response.Write(root.Name + " " +root.NodeDir + "<br/>");
PrintTree(root.Right, Current);
}
}
如果我理解正确,您想知道为什么您生成的列表没有通过其NodeDir
属性进行筛选。但是您的代码从不捕获或使用if
语句中Where
方法产生的列表。当你计算并使用它进行进一步过滤时,试着保留这个列表:
public List<Node> BuildTreeHierarchy(List<Node> node, int? pKey)
{
var nodesWithNodeDir = node.Where(n => n.NodeDir.Equals(true));
if (nodesWithNodeDir.Count() > 0)
{
return nodesWithNodeDir.Where(n => n.ParentKey == pKey)
.Select(n => new Node()
{
ID = n.ID,
Name = n.Name,
Feature = n.Feature,
NodeDir = n.NodeDir,
ParentKey = n.ParentKey,
Left = BuildTreeHierarchy(node, n.ID)
}).ToList();
}
}
foreach (DataRow animal in data.GetAnimals("select * from AnimalTbl").Rows)
{
Node newNode = new Node();
newNode.ID = Convert.ToInt32(animal["Id"]);
newNode.Name = animal["Name"].ToString();
newNode.Feature = animal["Feature"].ToString();
// newNode.NodeDir = animal["NodeDir"] == DBNull.Value ? (bool?)null : Convert.ToBoolean(animal["NodeDir"]);
newNode.NodeDir = Convert.ToBoolean(animal["NodeDir"]);
newNode.ParentKey = animal["ParentKey"] == DBNull.Value ? (int?)null : Convert.ToInt32(animal["ParentKey"]);
nodes.Add(newNode);
}
//AnimalTree = BuildTreeHierarchy(nodes, 1);
foreach (Node a in nodes)
{
List<Node> nodeSub = nodes.Where(n => n.ParentKey == a.ID).ToList<Node>();
a.Left = nodeSub.Where(n => n.NodeDir.Equals(false)).ToList<Node>();
a.Right = nodeSub.Where(n => n.NodeDir.Equals(true)).ToList<Node>();
}
return AnimalTree;
未使用函数。只需一个foreach循环即可分配每个项目的Left和Right属性。这对于性能和简单性都是最好的。
请注意,当左节点或右节点时,此代码会为您提供一个空集合,而不是null。