为什么声明不比较?列出项目,重新开始

时间:2016-01-11 21:19:18

标签: c# .net linq

我想添加仅具有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();
    }
}

结果:

承担错误 猴子真实 狼真实的 鸡肉假 鹳假


现在在其他部分我得到null exeption
*例如,我得到所有项目都是假的但是没有*

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>();
         }
    }
    }



在按钮点击事件中,我需要打印正确的节点列表,我得到空的曝光

    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);     
        }
    }

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您就会想知道为什么您生成的列表未被其NodeDir属性过滤。但是,您的代码永远不会捕获或使用Where语句中if方法生成的列表。尝试在计算时保留该列表并将其用于进一步过滤:

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();
    }
}

答案 1 :(得分:0)

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属性。这对性能和简单性都是最好的。

注意,当左侧或右侧节点时,此代码为您提供一个空集合,而不是空。