Question: I want to find last items of hierarchical tree.

```
class Node
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
}

```

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

```
    var list = new List<Node> {
        new Node {Id = 1, ParentId = 0, Name = "N 1"},
        new Node {Id = 2, ParentId = 1, Name = "N 1.1"},
        new Node {Id = 3, ParentId = 1, Name = "N 1.2"},
        new Node {Id = 4, ParentId = 2, Name = "N 1.1.1"},
        new Node {Id = 5, ParentId = 2, Name = "N 1.1.2"},
        new Node {Id = 6, ParentId = 2, Name = "N 1.1.3"}
    };

```

is this possible with c# or linq query?

Answer: 
Last nodes are those which are not other nodes parent.

Use this query:

```
var q = list.Where(x => list.All(y => y.ParentId != x.Id));

```
This is what you want 

```
var hasChild = list.Select(n => n.ParentId); 
var lastNodes = list.Where(n => !hasChild.Contain(n.Id));  

```