简体   繁体   中英

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

I have a CSV file which has 9 columns . (Date,DrawNumber,N1,N2,N3,N4,N5,N6,B1)

Below the above columns are 55 rows of data.

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.

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). Example: Number 33 = Found 12 Times, Number 14 found 11 Times,... (and so on for the remainder of the inputted numbers)

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. In other words, I have to match combinations per row (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数据示例:

create a class that matches the column so lets say having 9 columns means 9 properties.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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