I want to bring down the contents of the nested menus, but I failed.

Unal UN asked on June 16, 2019 13:52

Hello,

I want to bring down the contents of the nested menus, but I failed.

        Dictionary<Guid,string> parentMenuList = new Dictionary<Guid,string> ();

        Dictionary<Guid, string> childMenuList = new Dictionary<Guid, string>();


        var menu1 = DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US");


        foreach (var document in menu1)
        {
            parentMenuList.Add(document.DocumentGUID,document.DocumentName.ToString());


            if (document.Parent!=null)
            {
                var childMenu = DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US").InRelationWith(document.NodeGUID, "RelationProducts").Where(x => x.DocumentID == document.Parent.DocumentID);
                if (childMenu != null)
                {
                    foreach (var item in childMenu)
                    {
                        childMenuList.Add(item.DocumentGUID, item.DocumentName.ToString()); // null get data
                    }
                }
            }

Recent Answers


Roman Hutnyk answered on June 17, 2019 09:47

According to the code above you put parent document into childMenuList....

How many levels you want to go down? With those 2 dictionaries you could go just 2 levels, e.g. all parents and all children and you also loose relation between parent-child (you don't know who is parent for particular document).

So I'd try to build tree structure instead, some like this:

class MyClass {
    Guid DocGuid;
    string DocName;
    List<MyClass> Children;
} 

In addition following row gets all the documents of the site var menu1 = DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US");

It is ok if you really need to pull all the docs, otherwise I'd limit it to x levels.

Also next line of code finds searches for document parent and places it into childMenu variable: var childMenu = DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US").InRelationWith(document.NodeGUID, "RelationProducts").Where(x => x.DocumentID == document.Parent.DocumentID);

Either variable name is wrong or you need to adjust where condition to: var childMenu = DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US").InRelationWith(document.NodeGUID, "RelationProducts").Where(x => x.Parent.DocumentID== document.DocumentID);

Also not sure why you're checking related products...

Last, but not least, you're using LINQ in where condition - it does not work with Kentico Document Query, so SQL server executes this part: DocumentHelper.GetDocuments().OnSite("NewSite").Culture("en-US").InRelationWith(document.NodeGUID, "RelationProducts")

and the rest is processed on web server: .Where(x => x.Parent.DocumentID== document.DocumentID);

I'd recommend you to use Document Query only, so you get better performance.

1 votesVote for this answer Mark as a Correct answer

Unal UN answered on June 19, 2019 22:09

Hi,

exactly where will I name it InRelationWith(document.NodeGUID, "RelationProducts")

I'm not understand me

Is there a technical article on this topic? can you please help?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.