I have the following class

```
 public class Item 
{   
    public int Id { get; set; }           
    public int? ParentId { get; set; }
    public string Text { get; set; }
}

```

And the following class for tree view

```
public class TreeViewModel
{
    public TreeViewModel()
    {
        this.Children = new List<TreeViewModel>();
    }

    public int Id { get; set; }
    public int NodeId { get; set; }
    public string Text { get; set; }
    public bool Expanded { get; set; }
    public bool Checked { get; set; }

    public bool HasChildren
    {
        get { return Children.Any(); }
    }

    public IList<TreeViewModel> Children { get; private set; }
}

```

I will receive list of items and I would to convert it to tree.

the item that not have parent id it will the main node.

example: if i have the following items

```
item[0] = Id:0 Text:User ParentId:3

item[1] = Id:1 Text:Role ParentId:3

item[2] = Id:2 Text:SubUser ParentId:0

item[3] = Id:3 Text:Admin ParentId:null

item[4] = Id:4 Text:SuperAdmin ParentId:null

item[5] = Id:5 Text:Doha ParentId:4

```

the following item it will list of tree

[![enter image description here](https://i.stack.imgur.com/vgck3.png)](https://i.stack.imgur.com/vgck3.png)

I tried to make recursive function to do that , but i have no result