I have a large csv file where there are multiple values that belong to different people store in the same row. Below you can find a sample of this data.
| country | country2 | country3 | name1 | name2 | name3 | phone1 | phone2 | phone3 |
|---|---|---|---|---|---|---|---|---|
| USA | UK | Australia | Michael | Mitchell | David | 222 | 333 | 444 |
| Colombia | Paraguay | Bolivia | John | Chris | 555 | 7777 | ||
| Brazil | Germany | Japan | Silvia | Ana | 888 | 999 |
I want to split this data so that I can keep the first 3 columns untouched and only format the rest, meaning that I will keep the format for country, country2 and country3 but name and phone will appear just once. The idea is that each person from the same row will have at the end the same 3 countries but its data will be viewed on a separate row in order to look like this:
| country | country2 | country3 | name | phone |
|---|---|---|---|---|
| USA | UK | Australia | Michael | 222 |
| USA | UK | Australia | Mitchell | 333 |
| USA | UK | Australia | David | 444 |
| Colombia | Paraguay | Bolivia | ||
| Colombia | Paraguay | Bolivia | John | 555 |
| Colombia | Paraguay | Bolivia | Chris | 7777 |
| Brazil | Germany | Japan | Silvia | 888 |
| Brazil | Germany | Japan | Ana | 999 |
| Brazil | Germany | Japan |
I have seen some examples based on SQL but I am trying to accomplish this on C# since I need to have the data set on this particular way so that I can do some other things with it before sending it to the database. I currently storing the data into a datatable but I am not sure how can I make this change without affecting the incosistency of the data. Any ideas?
EDIT: This is the code that I have so far only to send this data to the datatable:
public static DataTable ConvertCSVtoDataTable(string strFilePath)
{
DataTable dt = new DataTable();
using (StreamReader sr = new StreamReader(strFilePath))
{
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dt.Rows.Add(dr);
}
}
return dt;
}