我有一个名为List<Dictionary<string, string>>
的{{1}}。我想使用LINQ返回3个键相等的值。
data
上面是一个示例列表。我希望能够返回List<Dictionary<string, string>> data = new List<Dictionary<string, string>>
{
new Dictionary<string, string>
{
{ "INDGEN", "100"},
{ "NO", "101"},
{ "DATE", "JUN"},
{ "PRD", "1"}
},
new Dictionary<string, string>
{
{ "INDGEN", "200"},
{ "NO", "101"},
{ "DATE", "JULY"},
{ "PRD", "2"}
},
new Dictionary<string, string>
{
{ "INDGEN", "300"},
{ "NO", "101"},
{ "DATE", "AUG"},
{ "PRD", "3"}
},
new Dictionary<string, string>
{
{ "NO", "400"},
{ "INDGEN", "101"},
{ "DATE", "SEP"},
{ "PRD", "4"}
},
new Dictionary<string, string>
{
{ "INDGEN", "500"},
{ "NO", "101"},
{ "DATE", "OCT"},
{ "PRD", "5"}
},
new Dictionary<string, string>
{
{ "NO", "600"},
{ "INDGEN", "101"},
{ "DATE", "NOV"},
{ "PRD", "6"}
}
};
,"INDGEN"
和"DATE"
值
当前实施:
"PRD"
当前成果:
data.SelectMany(x => x.Where(y => y.Key == "INDGEN" ||
y.Key == "DATE" || y.Key == "PRD" )).Select(z => z.Value);
以上内容返回正确的项目,但未将其分组为匿名集合。如何扩展以上内容以获得所需的结果,如下所示?
所需结果:
100
JUN
1
200
AUG
2
300
SEP
3
101
OCT
4
500
NOV
5
101
DEC
6
答案 0 :(得分:2)
您可以尝试通过每个词典进行记录:
-InputFile
答案 1 :(得分:0)
您可以尝试以下方法。请记住,您可以更改默认值
var anonCollection = data.Where(d => d.GetValueOrDefault("DATE", null) != null && d.GetValueOrDefault("INDGEN", null) != null && d.GetValueOrDefault("PRD", null) != null)
.Select(d => new []{ d.GetValueOrDefault("INDGEN", null), d.GetValueOrDefault("DATE", null), d.GetValueOrDefault("PRD", null) }).ToList();