Revious
Revious

Reputation: 8146

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

Upvotes: 1

Views: 34

Answers (2)

Sefe
Sefe

Reputation: 14007

Try this:

var qry =
    from node in nodes
    let nodeValues = new Node[] { node.Left, node.Right }
    from subnode in nodeValues
    select subnode;

This will eventually restult in a SelecMany, just in query syntax.

Upvotes: 0

Damith
Damith

Reputation: 63065

with SelectMany

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

Upvotes: 3

Related Questions