Sorting data received from a CSV files
Good day, I have been given an assignment that gets data off a CSV file. The CSV file contains the following entries: LName,FName,Dept,Grade,Gross Pay, Tax Paid, Net Pay.
I am able to retrieve the data and display it in the console window which is shown in my code below. However, I need help with displaying the results according to dept and getting the total net pay in each dept. There are three depts M, R, And S.
class DisplayEmpData
{
public void DisplayEmp()
{
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine("****************************************************************************");
Console.WriteLine("** **");
Console.WriteLine("** Payroll System **");
Console.WriteLine("** Employee Data Display **");
Console.WriteLine("****************************************************************************");
Console.WriteLine("****************************************************************************");
List<string> columns;
List<Dictionary<string, string>> myData = GetData(out columns);
foreach (string column in columns)
{
Console.Write("{0,-9}", column);
}
Console.WriteLine();
foreach (Dictionary<string, string> row in myData)
{
foreach (string column in columns)
{
Console.Write("{0,-9}", row[column]);
}
Console.WriteLine();
}
Console.ReadKey();
}
private static List<Dictionary<string, string>> GetData(out List<string> columns)
{
string line;
string[] stringArray;
char[] charArray = new char[] { ',' };
List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
columns = new List<string>();
try
{
FileStream aFile = new FileStream(@"..\..\EmployeeDetails.txt", FileMode.Open);
StreamReader sr = new StreamReader(aFile);
// Obtain the columns from the first line.
// Split row of data into string array
line = sr.ReadLine();
stringArray = line.Split(charArray);
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
columns.Add(stringArray[x]);
}
line = sr.ReadLine();
while (line != null)
{
// Split row of data into string array
stringArray = line.Split(charArray);
Dictiona开发者_JS百科ry<string, string> dataRow = new Dictionary<string, string>();
for (int x = 0; x <= stringArray.GetUpperBound(0); x++)
{
dataRow.Add(columns[x], stringArray[x]);
}
data.Add(dataRow);
line = sr.ReadLine();
}
sr.Close();
return data;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return data;
}
}
}
Can someone please help me?
Try loading the data into a DataTable like this:
private static DataTable GetEmployeeData()
{
string line;
string[] columnValues;
char[] columnSeperator = new char[] { ',' };
DataTable dt = new DataTable();
try
{
FileStream employeeFile = new FileStream(@"..\..\EmployeeDetails.txt", FileMode.Open);
StreamReader sr = new StreamReader(employeeFile);
// Obtain the columns from the first line.
line = sr.ReadLine();
columnValues = line.Split(columnSeperator);
for (int x = 0; x <= columnValues.GetUpperBound(0); x++)
{
// Add the column to the table
dt.Columns.Add(columnValues[x]);
}
line = sr.ReadLine();
while (line != null)
{
// Split row of data into string array
columnValues = line.Split(columnSeperator);
// add a new row with all of the values
dt.Rows.Add(columnValues);
}
sr.Close();
return dt;
}
catch (IOException ex)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return dt;
}
}
This makes it easier to play around with the data. You should be able to group the rows by department with something like this:
DataTable employeeData = GetEmployeeData();
// Create a view based on the data so that we can sort it by Dept
DataView view = employeeData.DefaultView;
view.Sort = "Dept";
if (!employeeData.Columns.Contains("Dept"))
{
Console.WriteLine("Employee Data file does not contain department column.");
return;
}
// Print the column headers
foreach (DataColumn column in employeeData.Columns)
{
Console.Write("{0,-9}", column.ColumnName);
}
Console.WriteLine();
// Print each row in order
foreach (DataRow row in view)
{
foreach (DataColumn column in employeeData.Columns)
{
Console.Write("{0,-9}", row[column]);
Console.WriteLine();
}
}
Console.ReadKey();
精彩评论