说我的数据如下:
Item 1
Item 2
Item 3
SubKey1:SubValue1
SubKey2:SubValue2
Item 4
SubKey1:SubValue1
Item 5
SubKey1:SubValue1
SubKey2:SubValue2
SubKey3:SubValue3
是否有支持此功能的数据结构或是否需要创建新类型?
答案 0 :(得分:4)
如果您想要的所有内容都是字符串,则可以使用:
Dictionary<string, Dictionary<string, string>>
您可以这样使用它:
var myItems = new Dictionary<string, Dictionary<string, string>>();
myItems.Add("Item 1", null);
myItems.Add("Item 2", null);
var subDictionary1 = new Dictionary<string, string>();
subDictionary1.Add("subKey1", "subValue1");
subDictionary1.Add("subKey2", "subValue2");
myItems.Add("Item 3", subDictionary1);
var subDictionary2 = new Dictionary<string, string>();
subDictionary2.Add("subKey1", "subValue1");
myItems.Add("Item 4", subDictionary2);
var subDictionary3 = new Dictionary<string, string>();
subDictionary3.Add("subKey1", "subValue1");
subDictionary3.Add("subKey2", "subValue2");
subDictionary3.Add("subKey3", "subValue3");
myItems.Add("Item 5", subDictionary3);
答案 1 :(得分:1)
这是一个非常模糊的问题。
但是,这听起来像是一个字典对象列表:
class MyItem : Dictionary<KeyType,ValueType> {
string MyItemData1;
int MyItemData2;
}
然后,只需要一个项目列表:
List<MyItem> ItemList = new List<MyItem>();
答案 2 :(得分:0)
从我看到你可以使用List<Dictionary<string, string>>
在前两项中你可以只有一个空字典(或空字符)。
但这取决于你想用它做什么。
答案 3 :(得分:0)
struct item
{
public string Id;
public Dictionary<string,string> MyValues;
}
然后创建项目列表
List<item> myItemList = new List<item>();
答案 4 :(得分:0)
它看起来像POCO给我的列表
public class SomeClass
{
public string Name {get;set;}
public List<KeyValuePair<string, string>> ChildValues {get;set;}
}
var myItems = new List<SomeClass> {
new SomeClass { Name = "Item 1"},
new SomeClass { Name = "Item 2"},
new SomeClass {
Name = "Item 3",
ChildValues = new List<KeyValuePair<string, string>>
{
new KeyValuePair("SubKey1", "SubValue1"),
new KeyValuePair("SubKey2", "SubValue2")
}
}
};
foreach (var item in myItems)
{
Console.WriteLine(item.Name);
if (item.ChildValues != null)
{
item.ChildValues.Each(i=>Console.WriteLine("\t{0}:{1}", i.Key, i.Value);
}
}
这是顶部未经测试的,但我认为它会很接近