У меня есть файл Excel (разделенный запятыми), два столбца, City
и Country
.Столбец A содержит страны, а столбец B - города.Поэтому в каждом ряду есть страна и город, расположенный в этой стране.
City Country
Madrid Spain
Barcelona Spain
Paris France
Valencia Spain
Rome Italy
Marseille France
Florence Italy
Мне интересно, как прочитать этот Excel в C # в типе Dictionary>, где ключом будет моя страна и значения города, поэтому после прочтения у меня будет следующее:
{
"Spain": ["Madrid", "Barcelona", "Valencia"],
"France": ["Paris", "Marseille"],
"Italy": ["Rome", "Florence"]
}
Пока я пытался создать этот класс:
class ReadCountryCityFile
{
Dictionary<string, List<string>> countrycitydict{ get; }
// constructor
public ReadCountryCityFile()
{
countrycitydict= new Dictionary<string, List<string>>();
}
public Dictionary<string, List<string>> ReadFile(string path)
{
using (var reader = new StreamReader(path))
{
List<string> listcountry = new List<string>();
List<string> listcity = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (line != "Country;City")
{
List<string> citieslist = new List<string>();
var values = line.Split(';');
citieslist .Add(values[0]);
string country= values[1];
countrycitydict[intents] = citieslist ;
}
}
return countrycitydict;
}
}
Но countrydict
не так, как ожидалось.Как я мог это сделать?
Как я мог решить это, если целое число
City Country
Madrid Spain
У меня было
City Country
Madrid Spain
Valencia