Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to have a code which can convert list<string> to Dictionary<string,string>

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

Dictionary<string,string> viaSwitchesResults = null;



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
Posted
Updated 29-Jan-15 23:15pm
v3
Comments
johannesnestler 30-Jan-15 6:37am    
use the samples, and instead of var write the exact type Name - finished
BillWoodruff 30-Jan-15 11:31am    
Without your specifying how you will transform each "whatever" in the .CSV file into the Key and Value needed to put an entry into a Dictionary: I don't think we can help you.

If i understand what you want...why don't you just use a foreach loop? Something like:
C#
Dictionary<string,string> viaSwitchesResults = new Dictionary<string,string>();
foreach (string fila in lstProcDetails)
{
   string[] data=fila.Split(',');
   viaSwitchesResults.Add(data[0],data[1]);
}
 
Share this answer
 
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#[^]
 
Share this answer
 
Pseudo code:
C#
create empty dictionary
foreach element in list do loop
  add element to dictionary: getKey(element), getValue(element)
end loop

Invent some GetKey and GetValue logic.

Converting to C# is left as exercise.

Cheers
Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900