Reputation: 8276
I'd like to extract a list of distinct possible values (name, type) of a nested object. How to get out the list of property names and their respective types from the following nested collection?
The data structure in C#:
Nodes (List<Node>)
- Node1:
-Properties (List<NodeProperty>) getter: Node1.Properties
-Property1:
-Name: "AAA" (string) getter Property1.Name
-Type: "string" (string) getter Property1.Type
Example:
MyNodes:
Node1:
Properties
Property1:
Name: "AAA"
Type: "string"
Property2:
Name: "BBB"
Type: "int"
Node2:
Properties
Property1:
Name: "CCC"
Type: "double"
Property2:
Name: "DDD"
Type: "double"
Result
List<KeyValuePair<string, string>> result;
// content of result after the Linq magic:
// AAA -> string
// BBB -> int
// CCC -> double
// DDD -> double
How can I extract this with Linq? Extracting the distinct names worked out like this:
List<string> names = MyNodes.SelectMany(n => n.Properties.Select(np => np.Name)).Distinct().ToList();
// res: [AAA, BBB, CCC, DDD]
Is this a good start?
Is there a single Linq query solution to this?
EDIT
Minimal Test Classes and data:
public class Node{
public List<NodeProperty> Properties;
}
public class NodeProperty
{
public string Name;
public string Type;
}
var MyNodes = new List<Node>
{
new Node {
Properties = new List<NodeProperty>
{
new NodeProperty {Name = "node1", Type="string"},
new NodeProperty {Name = "node2", Type="int"},
}
},
new Node {
Properties = new List<NodeProperty>
{
new NodeProperty {Name = "node3", Type="string"},
new NodeProperty {Name = "node4", Type="int"},
}
},
new Node {
Properties = new List<NodeProperty>
{
new NodeProperty {Name = "node1", Type="string"},
new NodeProperty {Name = "node2", Type="int"},
}
}
};
Upvotes: 3
Views: 2080
Reputation: 1618
Without actually running it up and debugging... it's definitely possible, and I think it will look similar to:
MyNodes.SelectMany(x => x.Nodes.SelectMany(y => y.Properties.Select(z =>new KeyValuePair<string, string>(z.Name, z.Type))).Distinct().ToList();
Edit:
Ran this up in LinqPad. My resulting query was:
nodes.SelectMany(n => n.Properties.Select(p => new KeyValuePair<string, string>(p.Name, p.Type))).Distinct().ToList();
Based on the class & initialization below:
public class Node{
public List<NodeProperty> Properties;
}
public class NodeProperty
{
public string Name;
public string Type;
}
var nodes = new List<Node>
{
new Node {
Properties = new List<NodeProperty>
{
new NodeProperty {Name = "node1", Type="string"},
new NodeProperty {Name = "node2", Type="int"},
}
},
new Node {
Properties = new List<NodeProperty>
{
new NodeProperty {Name = "node3", Type="string"},
new NodeProperty {Name = "node4", Type="int"},
}
},
};
Upvotes: 3