Revious Revious - 16 days ago 5
C# Question

Linq: creating a list taking left and right child of every Node in a List

I have a List nodes where Node is

public class Node
{
public Node Left
public Node Right
}


I'd like to extract a list which looks like this:

{nodes[0].Left, nodes[0].Right, nodes[1].Left, nodes[1].Right, ...}


I am trying with Aggregate, Concat, Select and SelectMany

Answer

with SelectMany

var result = list.SelectMany(x=>new List<Node>(){x.Left, x.Right}).ToList();