【发布时间】:2016-02-07 19:35:13
【问题描述】:
我正在尝试逐行迭代读取数据的文件。阅读后我想存储在字典中。键是唯一的,但值具有键的值列表(每个键可能有 50 个值)。但是,当迭代键随机出现时。如何为每个键创建一个新列表并将值存储在相应的列表中,当下次相同的键出现时......如何将所有这些新键和相应的列表存储在字典中。请告诉我..
这是我的代码的解释..
Dictionary<String,List<PropertyCollection>> dict = new Dictionary<String,List<PropertyCollection>>();
List<String> list1 = new List<String>();
//Here I am iterating the each record and getting the type and id
for (i=1;i<datarr.length -1;i++){
String type = datarr[3].Trim();
String id = datarr[1].Trim();
//here I am checking the key in map adding
if(dict.ContainsKey(type)){
//I need help here if the key is not there create a new record with key as "type" and values as "id"s. All the values of same type should add in a list.If any new type comes in iteration it should add as new entry and having key and values are Ids in a list format.
I stuck here ..
}
}
我不知道该文件中有多少“类型”。所以我需要动态构建列表。
Please help .
【问题讨论】:
-
你试过了吗?
-
如果你至少要尝试一些东西,这实际上很容易完成..现在来吧
-
我已经尝试了一些东西。我会在这里更新我的代码
标签: c# collections