문제

I'm creating a special tree algorithm and I need a bit of help with the code that I currently have, but before you take a look on it please let me explain what it really is meant to do.

I have a tree structure and I'm interacting with a node (any of the nodes in the tree(these nodes are Umbraco CMS classes)) so upon interaction I render the tree up to the top (to the root) and obtain these values in a global collection (List<Node> in this particular case). So far, it's ok, but then upon other interaction with another node I must check the list if it already contains the parents of the clicked node if it does contain every parent and it doesn't contain this node then the interaction is on the lowest level (I hope you are still with me?).

Unfortunately calling the Contains() function in Umbraco CMS doesn't check if the list already contains the values which makes the list add the same values all over again even through I added the Contains() function for the check.

Can anyone give me hand here if he has already met such a problem? I exchanged the Contains() function for the Except and Union functions, and they yield the same result - they do contain duplicates.

var currentValue = (string)CurrentPage.technologies;
    List<Node> globalNodeList = new List<Node>();
    string[] result = currentValue.Split(',');
    foreach (var item in result)
    {
        var node = new Node(int.Parse(item));
        if (globalNodeList.Count > 0)
        {
            List<Node> nodeParents = new List<Node>();

            if (node.Parent != null)
            {
                while (node != null)
                {
                    if (!nodeParents.Contains(node))
                    {
                        nodeParents.Add(node);
                    }
                    node = (Node)node.Parent;
                }
            }

            else { globalNodeList.Add(node); }

            if (nodeParents.Count > 0)
            {
                var differences = globalNodeList.Except<Node>(globalNodeList);
                globalNodeList = globalNodeList.Union<Node>(differences).ToList<Node>();
            }
        }
        else
        {
            if (node.Parent != null)
            {
                while (node != null)
                {
                    globalNodeList.Add(node);
                    node = (Node)node.Parent;
                }
            }
            else
            {
                globalNodeList.Add(node);
            }
        }
    }
}
도움이 되었습니까?

해결책

If I understand your question, you only want to see if a particular node is an ancestor of an other node. If so, just (string) check the Path property of the node. The path property is a comma separated string. No need to build the list yourself.

Just myNode.Path.Contains(",1001") will work.

Small remarks.

  1. If you are using Umbraco 6, use the IPublishedContent instead of Node.
  2. If you would build a list like you do, I would rather take you can provide the Umbraco helper with multiple Id's and let umbraco build the list (from cache).

For the second remark, you are able to do this:

var myList = Umbraco.Content(1001,1002,1003);

or with a array/list

var myList = Umbraco.Content(someNode.Path.Split(','));

and because you are crawling up to the root, you might need to add a .Reverse()

More information about the UmbracoHelper can be found in the documentation: http://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/

If you are using Umbraco 4 you can use @Library.NodesById(...)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top