将 CSV 文件读入字符串列表,然后循环以识别项目 [英] read CSV file into a List of strings and then loop to identify an item

查看:24
本文介绍了将 CSV 文件读入字符串列表,然后循环以识别项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用 C# 学习 Windows 窗体,并且在将我的控制台应用程序重写为窗口窗体应用程序时遇到了很多麻烦.这是我在控制台应用程序中的代码:

I'm just learning Windows Forms with C# and I'm having a lot of trouble rewriting my Console app into a window forms app. Here is my code in the 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 文件,然后将其添加到字符串列表中,然后搜索员工类型",然后根据该值循环它进行计算,然后打印出所有值.

I am reading from a CSV .txt file and then I add it to a list of strings and I search for the "employee type" and then based on that value I loop it to do a calculation and then print out all the values.

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

I was able to read the csv file into a textbox in windows form, but I cannot figure out how to loop it and read the specific value and then continue with my calculation. here is what I have for my console windows form:

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,但我还没有那么远.任何帮助将不胜感激.

Where can I add my loop? I tried using as much of my code in both formats, but I know console.writeline needs to be changed for windows form, but I haven't gotten that far yet. Any assistance would be very much appreciated.

推荐答案

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

First of all it is easier to use Lambda Expressions. So your code would look like this:

        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"));
        }

这篇关于将 CSV 文件读入字符串列表,然后循环以识别项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆