Revious Revious - 1 year ago 64
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 Source

with SelectMany

var result = list.SelectMany(x=>new List<Node>(){x.Left, x.Right}).ToList();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download