美好的一天, 我获得了一项从CSV文件中获取数据的作业。 CSV文件包含以下条目: LName,FName,Dept,Grade,Gross Pay,Tax Paid,Net Pay。
我能够检索数据并将其显示在控制台窗口中,该窗口显示在下面的代码中。但是,我需要帮助根据部门显示结果并获得每个部门的总净工资。有三个部分M,R和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);
Dictionary<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;
}
}
}
有人可以帮帮我吗?
答案 0 :(得分:1)
尝试将数据加载到DataTable中,如下所示:
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;
}
}
这样可以更轻松地使用数据。您应该能够按部门对行进行分组:
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();