转换列表< String> to dictionary< string,String>不使用Linq和Var? [英] Convert List<String> to dictionary<string,String> without using Linq and Var?

查看:87
本文介绍了转换列表< String> to dictionary< string,String>不使用Linq和Var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个可以转换列表< string>的代码to Dictionary< string,string>



I need to have a code which can convert list<string> to Dictionary<string,string>

String strFilePath = Directory.GetCurrentDirectory() + "\\" + procedureName.Trim() + ".csv";
List<string> lstProcDetails;
 lstProcDetails = ReadCSV(strFilePath);

Dictionary<string,string> viaSwitchesResults = null;







I我正在使用.NET framework 2.0,我不能使用Var关键字。我的要求是从CSV文件中将项目放入列表中,然后将其添加到字典中以便稍后进行搜索。我看过一些带有Var关键字的参考示例。请帮助




I am using .NET framework 2.0 where i cannot use Var keyword. My requirement is to get the items into list from CSV file and then add it into dictionary for search later. I have seen some reference examples with "Var" keyword. Please help

推荐答案

如果我明白你想要什么...为什么你不使用foreach循环?类似于:
If i understand what you want...why don't you just use a foreach loop? Something like:
Dictionary<string,string> viaSwitchesResults = new Dictionary<string,string>();
foreach (string fila in lstProcDetails)
{
   string[] data=fila.Split(',');
   viaSwitchesResults.Add(data[0],data[1]);
}


您似乎很难理解数据结构。字典和列表是具有不同用法的数据结构。您还没有提到将列表转换为字典的规则。还要记住,没有可用于将列表转换为字典的默认转换。



我的建议是了解数据结构的含义和用法。你可以从这里开始有什么区别列表<>和字典<>在c# [ ^ ]
It seems you have difficulty in understanding data structures. Dictionary and list are datastructures that have different usage. You have not mentioned anything about rules that will convert a list to dictionary. Do also bear in mind that there is no default conversion available that will cast a list into dictionary.

My advice is to understand the meaning and usage of data structures. You can start from here what is the difference between list<> and dictionary<> in c#[^]


伪代码:

Pseudo code:
create empty dictionary
foreach element in list do loop
  add element to dictionary: getKey(element), getValue(element)
end loop



发明一些GetKey和GetValue逻辑。



转换为C#留作练习。



干杯

Andi


Invent some GetKey and GetValue logic.

Converting to C# is left as exercise.

Cheers
Andi


这篇关于转换列表&lt; String&gt; to dictionary&lt; string,String&gt;不使用Linq和Var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆