如何将嵌套字典展平为一些对象列表(SomeObject
在以下示例中),这些对象应包含这些字典的键?
例如:让我们有一个以下类型的字典
var nestedDictionary = new Dictionary<int, Dictionary<int, string>>();
那么,让我们上这堂课
public class SomeObject
{
public int var1;
public int var2;
public string someStringVar;
}
如何转换nestedDictionary
为List<SomeObject>
wherevar1
是外部字典var2
的键,是内部字典的键,是内部字典someStringVar
的字符串值?
本质上,我如何转移这个:
nestedDict[0][0] = "foo";
nestedDict[0][1] = "bar";
nestedDict[0][2] = "foo1";
nestedDict[1][0] = "bar1";
nestedDict[1][1] = "foo2";
nestedDict[1][2] = "bar2";
对此(在伪 C# 中只是为了可视化它)
objList[0] = SomeObject { var1 = 0, var2 = 0, someStringVar = "foo" }
objList[1] = SomeObject { var1 = 0, var2 = 1, someStringVar = "bar" }
objList[2] = SomeObject { var1 = 0, var2 = 2, someStringVar = "foo1" }
objList[3] = SomeObject { var1 = 1, var2 = 0, someStringVar = "bar1" }
objList[4] = SomeObject { var1 = 1, var2 = 1, someStringVar = "foo2" }
objList[5] = SomeObject { var1 = 1, var2 = 2, someStringVar = "bar2" }
使用 LINQ?