C# Linq获取嵌套元素的不同值
我想提取嵌套对象的不同可能值(名称、类型)的列表。 如何从以下嵌套集合中获取属性名称及其各自类型的列表 C#中的数据结构: 结果C# Linq获取嵌套元素的不同值,c#,linq,C#,Linq,我想提取嵌套对象的不同可能值(名称、类型)的列表。 如何从以下嵌套集合中获取属性名称及其各自类型的列表 C#中的数据结构: 结果 List<KeyValuePair<string, string>> result; // content of result after the Linq magic: // AAA -> string // BBB -> int // CCC -> double // DDD -> double 列表
List<KeyValuePair<string, string>> result;
// content of result after the Linq magic:
// AAA -> string
// BBB -> int
// CCC -> double
// DDD -> double
列表结果;
//Linq魔术后的结果内容:
//AAA->字符串
//BBB->int
//CCC->double
//DDD->double
我如何用Linq提取这个?
提取不同名称的过程如下所示:
List<string> names = MyNodes.SelectMany(n => n.Properties.Select(np => np.Name)).Distinct().ToList();
// res: [AAA, BBB, CCC, DDD]
List Name=MyNodes.SelectMany(n=>n.Properties.Select(np=>np.Name)).Distinct().ToList();
//决议:[AAA、BBB、CCC、DDD]
这是一个好的开始吗
是否有一个单一的Linq查询解决方案
编辑
最小测试类别和数据:
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"},
}
}
};
公共类节点{
公共财产清单;
}
公共类节点属性
{
公共字符串名称;
公共字符串类型;
}
var MyNodes=新列表
{
新节点{
属性=新列表
{
新节点属性{Name=“node1”,Type=“string”},
新节点属性{Name=“node2”,Type=“int”},
}
},
新节点{
属性=新列表
{
新节点属性{Name=“node3”,Type=“string”},
新节点属性{Name=“node4”,Type=“int”},
}
},
新节点{
属性=新列表
{
新节点属性{Name=“node1”,Type=“string”},
新节点属性{Name=“node2”,Type=“int”},
}
}
};
没有实际运行和调试。。。这绝对是可能的,我认为它看起来类似于:
MyNodes.SelectMany(x=>x.Nodes.SelectMany(y=>y.Properties.Select(z=>newkeyvaluepair(z.Name,z.Type))).Distinct().ToList();
编辑:
在LinqPad中运行此操作。我的查询结果是:
nodes.SelectMany(n=>n.Properties.Select(p=>newkeyvaluepair(p.Name,p.Type)).Distinct().ToList();
基于以下类别和初始化:
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"},
}
},
};
公共类节点{
公共财产清单;
}
公共类节点属性
{
公共字符串名称;
公共字符串类型;
}
var节点=新列表
{
新节点{
属性=新列表
{
新节点属性{Name=“node1”,Type=“string”},
新节点属性{Name=“node2”,Type=“int”},
}
},
新节点{
属性=新列表
{
新节点属性{Name=“node3”,Type=“string”},
新节点属性{Name=“node4”,Type=“int”},
}
},
};
请您将数据作为有效的C#代码提供,以便我们可以复制粘贴并运行。添加了一些示例数据和类。该示例代码看起来很熟悉:)提供的答案有问题吗?是@AndrewP感谢示例代码!:)相同类型的列表(具有相同属性名称和类型的节点)在列表中重复,我希望它是不同的。你能调整吗?这看起来不错!但是,当两个节点具有相同的属性时,相同的键值将在结果中重复。是的,您可以在.ToList()之前或之后添加.distinct()!非常感谢。
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"},
}
}
};
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"},
}
},
};