Read the column values from the n-th row using CSVHelper class ?

JSS

New member
Joined
Oct 12, 2022
Messages
1
Programming Experience
Beginner
How to read the column values from a nth row ?
Suppose I have a csv file with values as seen in the attached image.
The first column is not having a header name. We can consider that column as rowindex.
But If I give 7 as the row value, how to take corresponding AA and CC column values ?
[avoid BB column].

Could anyone help me how to do this in C# ??
 

Attachments

  • csvrow.png
    csvrow.png
    10 KB · Views: 37
Have you looked at the documentation for that CSVHelper class?

Can you provide us a link to the CSVHelper class so that we can also look at the documentation?
 
Assuming that you are using this CSVHelper:

then you would configure the class map to not use a header row:

And then you would just use Skip() to skip the necessary number of rows to get to the row that you are interested in assuming you truly want to get to the n-th row as you stated in your title.

On the other hand, if you actually want to respect the values in that first column and pick the row that has the matching value, then use Where() to find the matching value.
 
On the other hand, if you actually want to respect the values in that first column and pick the row that has the matching value, then use Where() to find the matching value.
If it's known to be a single row then you'd probably call First or FirstOrDefault rather than Where. The latter would read every row, unless you called one of the others after it. The first two will stop reading once they find a match, assuming that CSVHelper is written to support that.
 
According to the docs it is lazy and will only return one row at a time.
 
I should clarify, if you were to call Where and use a foreach loop over the results then you could exit the loop when a match was found. If you were to call Where and then ToList or ToArray then you'd read to the end, probably needlessly. You could call Where and then First or FirstOrDefault but there's no point calling both because the latter too will accept the same filters that Where will.
 
Docs: Enumerate Class Records | CsvHelper

Code:

C#:
// See https://aka.ms/new-console-template for more information
using CsvHelper;
using CsvHelper.Configuration.Attributes;
using System.Globalization;

Console.WriteLine("Hello, World!");

var csvStr = @",AA,BB,CC
1,a,b,c
2,aa,bb,cc
3,aaa,bbb,ccc
4,aaaa,bbbb,cccc
5,aaaaa,bbbbb,ccccc
6,aaaaaa,bbbbbb,cccccc
7,aaaaaaa,bbbbbbb,ccccccc
8,aaaaaaaa,bbbbbbbb,cccccccc";

using (var reader = new StringReader(csvStr))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
    var record = new CSVItem();
    var records = csv.EnumerateRecords(record);

    var seventh = records.Skip(6).First();

    Console.WriteLine(seventh.AA);
}



class CSVItem
{
    [Index(0)] public int Num { get; set; }
    public string AA { get; set; }
    public string BB { get; set; }
    public string CC { get; set; }
}

Result:


1665748741527.png
 
Back
Top Bottom