Binding data to a TreeView The entire flow depends on parameters. It's all in the same method with an if/else, but I want to make the code a bit more clear. How can I functionally decompose this method into many?

private void BindTreeView(IEnumerable&lt;Opcion&gt; OptionList, TreeNode parentNode)
{
    var nodes = OptionList.Where(x =&gt; parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));

    if (_pagemode != BoPage.PageMode.View)
    {
        foreach (Opcion node in nodes)
        {
            TreeNode newNode = new TreeNode();
            newNode.Text = (node.ParentID == 0) ? string.Format(@"&lt;span class=""Tema"" id=""Tema{0}""&gt;{1}&lt;/span&gt;", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"&lt;span class=""SubTema""&gt;{1}&lt;/span&gt;", node.ParentID.ToString(), node.Descripcion.ToString());                      
            newNode.Value = node.OpcionID.ToString();
            newNode.ToolTip = node.Descripcion.ToString();

            if (parentNode == null)
            {
                TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
            }
            else
            {
                if (node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0)
                {
                    newNode.Checked = true;
                    parentNode.Expand();
                }
                parentNode.ChildNodes.Add(newNode);
            }
            BindTreeView(OptionList, newNode);               
        }

    }
    else
    {
        foreach (Opcion node in nodes)
        {
            if (ExistKeyWord(node))
            {                
                TreeNode newNode = new TreeNode();
                newNode.Text = (node.ParentID == 0) ? string.Format(@"&lt;span class=""Tema"" id=""Tema{0}""&gt;{1}&lt;/span&gt;", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"&lt;span class=""SubTema""&gt;{1}&lt;/span&gt;", node.ParentID.ToString(), node.Descripcion.ToString());                      
                newNode.Value = node.OpcionID.ToString();
                newNode.ToolTip = node.Descripcion.ToString();

                if (parentNode == null )
                {
                    TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
                }
                else
                {
                    if (node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0)
                    {
                        newNode.Checked = true;
                        parentNode.Expand();
                    }

                    if (node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0)
                    { 
                        parentNode.ChildNodes.Add(newNode);
                    }

                }
                BindTreeView(OptionList, newNode);
            }
        }

    }


}
I want to make the code a bit more clear


I would start by removing one of the if branches and customize the loop to do the job according to the parameters because both loop are nearly identical.

You can create two helper variables for the conditions and act appropriately:

var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);

if (!canAddTreeNote)
{
    continue;
}


and for adding child nodes something like this:

var canAddChildNote = 
    _pagemode == BoPage.PageMode.View 
    &amp;&amp; ExistKeyWord(node)
    &amp;&amp; node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0;

if (canAddChildNote) 
{
    parentNode.ChildNodes.Add(newNode);
}


by their powers combined you'll get:

private void BindTreeView(IEnumerable&lt;Opcion&gt; OptionList, TreeNode parentNode)
{
    var nodes = OptionList.Where(x =&gt; parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Value));


    foreach (Opcion node in nodes)
    {

        var canAddTreeNote = _pagemode != BoPage.PageMode.View || ExistKeyWord(node);

        if (!canAddTreeNote)
        {
            continue;
        }

        TreeNode newNode = new TreeNode();
        newNode.Text = (node.ParentID == 0) ? string.Format(@"&lt;span class=""Tema"" id=""Tema{0}""&gt;{1}&lt;/span&gt;", node.OpcionID.ToString(), node.Descripcion.ToString()) : string.Format(@"&lt;span class=""SubTema""&gt;{1}&lt;/span&gt;", node.ParentID.ToString(), node.Descripcion.ToString());                      
        newNode.Value = node.OpcionID.ToString();
        newNode.ToolTip = node.Descripcion.ToString();

        if (parentNode == null)
        {
            TreeViewPalabrasClaveOpciones.Nodes.Add(newNode);
        }
        else
        {
            if (node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0)
            {
                newNode.Checked = true;
                parentNode.Expand();
            }

            var canAddChildNote = 
                _pagemode == BoPage.PageMode.View 
                &amp;&amp; ExistKeyWord(node)
                &amp;&amp; node.PalabrasClaveOpciones.Where(c =&gt; c.KeywordID == _idKeyWord).Count() &gt; 0;

            if (canAddChildNote) 
            {
                parentNode.ChildNodes.Add(newNode);
            }
        }
        BindTreeView(OptionList, newNode);               
    }
}





  How can I functionally decompose this method into many?


When you apply the above refactoring I think it's no longer necessary to create more methods.