简体   繁体   English

如何将CSV数据存储到C#中的数组中?

[英]How to store CSV data into an array in C#?

I have a CSV file which has 9 columns . 我有一个包含9列的CSV文件。 (Date,DrawNumber,N1,N2,N3,N4,N5,N6,B1) (日期,DrawNumber,N1,N2,N3,N4,N5,N6,B1)

Below the above columns are 55 rows of data. 在上述列下方是55行数据。

The data in the rows are separated by the (,) delimiter. 行中的数据由(,)分隔符分隔。


1) In a console application, the user will input 6/7 numbers which will be stored. 1)在控制台应用程序中,用户将输入6/7的数字将被存储。

2.) How do I read and save the CSV data into array(s) in such a way where I can count how many times each number (of the users inputted set of 6 or 7 numbers) were found in the entire CSV (from these columns: N1,N2,N3,N4,N5,N6,B1). 2)如何读取CSV数据并将其保存到数组中,这样我可以计算在整个CSV中(从用户输入的6或7个数字中)每个数字被发现了多少次这些列:N1,N2,N3,N4,N5,N6,B1)。 Example: Number 33 = Found 12 Times, Number 14 found 11 Times,... (and so on for the remainder of the inputted numbers) 例如:数字33 =找到12次,数字14找到11次,...(以此类推,其余输入的数字则如此)

3.)From the 6 or 7 numbers that the user will input, I will also have to see how many numbers (of the user's input) will match with each row of the CSV. 3)从用户将要输入的6或7个数字中,我还必须查看(用户输入的)多少个数字将与CSV的每一行匹配。 In other words, I have to match combinations per row (N1,N2,N3,N4,N5,N6,B1) 换句话说,我必须匹配每行的组合(N1,N2,N3,N4,N5,N6,B1)


What would be the best way of reading and storing the data to allow for such data manipulations? 读取和存储数据以进行此类数据操作的最佳方法是什么? (your sample code will be appreciated) ? (您的示例代码将不胜感激)?

Thanks in advance. 提前致谢。

EXAMPLE OF CSV DATA: CSV数据示例:

CSV数据示例:

create a class that matches the column so lets say having 9 columns means 9 properties. 创建一个与列匹配的类,因此假设有9列意味着9个属性。

public class CsvData
{
    public string column1 { get; set; }
    public string column2 { get; set; }
    public string column3 { get; set; }
    public string column4 { get; set; }
    public string column5 { get; set; }
    public string column6 { get; set; }
    public string column7 { get; set; }
    public string column8 { get; set; }
    public string column9 { get; set; }
}

when now lest map the csv file into list of CsvData object 现在以免将csv文件映射到CsvData对象的列表中

public List<CsvData> ReadCsv(string file)
{
    List<CsvData> collection = new List<CsvData>();
    using (var streamReader = new StreamReader(file))
    {
        while (!streamReader.EndOfStream)
        {
            string[] columns = streamReader.ReadLine().Split(';');

            CsvData data = new CsvData();
            data.column1 = columns[0];
            data.column2 = columns[1];
            data.column3 = columns[2];
            data.column4 = columns[3];
            data.column5 = columns[4];
            data.column6 = columns[5];
            data.column7 = columns[6];
            data.column8 = columns[7];
            data.column9 = columns[8];

            collection.Add(data);
        }
    }

    return collection;
}

Now for calculating rows containing a number or term 现在用于计算包含数字或项的行

public int CountRowsWithTerm(List<CsvData> collection, string term)
{
    List<CsvData> RowsContainingTheTerm = collection.Where(row => row.column1 == term || row.column2 == term || row.column3 == term ||
                            row.column4 == term || row.column5 == term || row.column6 == term ||
                            row.column7 == term || row.column8 == term || row.column9 == term).ToList();
    return RowsContainingTheTerm.Count();
}

hope this helped 希望这有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM