将CSV文件读入字符串列表,然后循环以标识项目

时间:2018-04-05 04:40:53

标签: c# visual-studio

我正在使用C#学习Windows窗体,而且我在将控制台应用程序重写为窗口表单应用程序时遇到了很多麻烦。以下是我在Console App中的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        const string FILENAME = @"C:\Users\annage\Desktop\SCHOOL\POS409\data.txt"; //path for CSV file        

        List<List<string>> EmployeesInfo = new List<List<string>>();
        string inputLine = "";
        StreamReader reader = new StreamReader(FILENAME);
        int whileLoopRan = 0;

        while ((inputLine = reader.ReadLine()) != null)     //enters items into array                                                             
        {
            if (whileLoopRan == 0)
            {
                whileLoopRan++;
                continue;
            }
            whileLoopRan = 1;
            List<string> inputArray = inputLine.Split(new char[] { ',' }).ToList();//remove extra spaces
            inputArray = inputArray.Select(x => x.Trim()).ToList();
            EmployeesInfo.Add(inputArray);
        }

        List<EmployeeTaxes> EmployeeList = new List<EmployeeTaxes>();

        foreach (List<string> EmployeeInfo in EmployeesInfo)
        {
            String employeeType;
            employeeType = EmployeeInfo[5];
            EmployeeTaxes employeeTaxType = new EmployeeTaxes();
            employeeTaxType.GrossPay = double.Parse(EmployeeInfo[3]);

            if (employeeType == ("W2"))
            {
                double taxes;
                taxes = .07;
                employeeTaxType.taxes = double.Parse(EmployeeInfo[3]) * .07;
                employeeTaxType.totaltaxes = employeeTaxType.taxes * 12;
            }  
            else if (employeeType == ("1099"))
            {
            }
            EmployeeList.Add(employeeTaxType);
        }
        DisplayData(EmployeeList, EmployeesInfo);
        Console.ReadLine();
    }



    private void DisplayData(List<EmployeeTaxes> employeeList, List<List<string>> employeesInfo)
    {

        for (int i = 0; i < employeeList.Count; i++)
        {
            List<string> Row = employeesInfo[i];
            string fullName = Row[0];
            string address = Row[1];
            string employeeType = Row[5];
            string developerType = Row[6];

            EmployeeTaxes taxRow = employeeList[i];
            double grossPay = taxRow.GrossPay;
            double taxes = taxRow.taxes;
            double totalGrossPay = taxRow.TotalGrossPay();
            double annualTaxes = taxRow.totaltaxes;
            double netPay = taxRow.Netpay();

            Console.WriteLine("Welcome, !", fullName); // shows greeting and users name
            Console.WriteLine("Address: ", address);  //shows address entered
            Console.WriteLine("Gross Pay: $", grossPay);  // Shows gross pay entered
            Console.WriteLine("Employee Type: ", employeeType);
            Console.WriteLine("Monthly taxes are 7%");  ("Monthly Taxes Paid are: $" + taxes.ToString("N2"));  // calculated the taxes paid monthly

            Console.WriteLine("Annual Gross Pay: $" + totalGrossPay.ToString("N2"));    // calulates gross pay * 12months

            Console.WriteLine("Annual Taxes Paid: $" + annualTaxes.ToString("N2"));     // calulates taxes * 12months

            Console.WriteLine("NetPay: $" + netPay.ToString("N2"));
            Console.WriteLine("Developer Type: ", developerType);
        }
    }
}

我正在读取CSV .txt文件,然后将其添加到字符串列表中,然后搜索&#34;员工类型&#34;然后基于该值,我循环它进行计算,然后打印出所有值。

我能够在Windows窗体中将csv文件读入文本框,但我无法弄清楚如何循环它并读取特定值然后继续我的计算。 这是我的控制台窗口形式:

public Form1()
{
    InitializeComponent();
    this.listBox3.SelectionMode = SelectionMode.MultiSimple;
    ReadingCSVFile();
}

public void ReadingCSVFile()
{
    List<List<string>> EmployeesInfo = new List<List<string>>();
    string inputLine = "";

     using (StreamReader sr = new StreamReader(@"C:\Users\annage\Desktop\SCHOOL\POS409\data.txt"))
     {
         string line;
         while ((line = sr.ReadLine()) != null)
             listBox3.Items.Add(line);
         List<string> inputArray = inputLine.Split(new char[] { ',' }).ToList();
         inputArray = inputArray.Select(x => x.Trim()).ToList();
         EmployeesInfo.Add(inputArray);
     }
}

private void DisplayData(List<EmployeeTaxes> employeeList, List<List<string>> EmployeesInfo)
{

    for (int i = 0; i < employeeList.Count; i++)
    {
         List<string> Row = EmployeesInfo[i];
         string fullName = Row[0];
         string address = Row[1];
         string employeeType = Row[5];
         string developerType = Row[6];

         EmployeeTaxes taxRow = employeeList[i];
         double grossPay = taxRow.GrossPay;
         double taxes = taxRow.taxes;
         double totalGrossPay = taxRow.TotalGrossPay();
         double annualTaxes = taxRow.totaltaxes;
         double netPay = taxRow.Netpay();
    }
}

我可以在哪里添加循环?我尝试在两种格式中使用尽可能多的代码,但我知道需要为windows窗体更改console.writeline,但我还没有达到目前为止。 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

首先,使用Lambda表达式更容易。 所以你的代码看起来像这样:

        public void Test()
        {
            List<string> myList = new List<string>();
            using (var reader = new StreamReader(@"2018-03-16.csv"))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();

                    var values = line.Split(';');

                    foreach (string item in values)
                    {
                        myList.Add(item);
                    }
                }
            }

            this.FindItem(myList);
        }

        private void FindItem(List<string> myList)
        {
            // if you want the value
            var match = myList.FirstOrDefault(stringToCheck => stringToCheck.Equals("your item to check"));

            // if you just want to check if it exists
            var match2 = myList.Any(stringToCheck => stringToCheck.Equals("your item to check"));
        }