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