[Solved] Search value in csv file using c# [closed]


You can create a function to do this task as below:

String GetAddress(String searchName)
{       
   var strLines=File.ReadLines("filepath.csv");
   foreach(var line in strLines)
   {
      if(line.Split(',')[1].Equals(searchName))
         return line.Split(',')[2];
   }

   return "";
}

you can call the above function as below:

String peterAddress=GetAddress("Peter");

EDIT:

        String address="";
        Dictionary<String, String> dict_Name_Address = new Dictionary<string, string>();
        var lines=File.ReadLines("FileName.csv");
        foreach (var line in lines)
        {
            dict_Name_Address.Add(line.Split(',')[1],line.Split(',')[2]);
        }
        if(dict_Name_Address.ContainsKey(searchKey))
         address = dict_Name_Address[searchKey];

15

solved Search value in csv file using c# [closed]