1
我有一个类A,其中包含由类B描述的键/值对的数组。其中一个键称为“name”。我想使用LINQ建立一个Dictionary<string, Dictionary<string, string>>
其中第一库的关键是B数组中称为“名”属性和值是一个包含B.从Linq使用数组构建字典
public class A
{
public int a;
public B[] b;
}
public class B
{
public string key;
public string value;
}
A[] a = new A[]
{
new A { a = 1, b = new B[] { new B { key = "name", value = "n1"}, new B { key = "test", value = "t1" } }, },
new A { a = 2, b = new B[] { new B { key = "name", value = "n2"}, new B { key = "test", value = "t2" } }, },
};
所以第二词典其实我想建立在阵列“A”使用LINQ以下字典:
Dictionary<string, Dictionary<string, string>> d = new Dictionary<string, Dictionary<string, string>>()
{
{ "n1", new Dictionary<string, string>() { { "name", "n1" }, { "test", "t1" } } },
{ "n2", new Dictionary<string, string>() { { "name", "n2" }, { "test", "t2" } } },
};
酷!参数“名称”可以随机放置在数组中(不一定是第一个元素)。我如何修改表达式来应对这个问题? – user957479
@ user957479更新 – xanatos
谢谢,当你看到结果时,它总是看起来很容易;-) – user957479