What is the best way to get the list of column names using CsvHelper?

B A :

I am trying to use CsvHelper for a project. I went through the documentation but I couldn't find a way to read all the column names with a single method. How can I get a list of all column header names easily with CsvHelper? I am doing it like this currently but I assume there is a better way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using System.IO;

namespace Kg
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var reader = new StreamReader(@"myfile.csv"))
            {
                var csv = new CsvReader(reader);

                csv.Read();                  
                var has_header = true;
                var csv_headers = new List<string>();
                var header_index = 0;
                while (has_header)
                {
                    has_header = csv.TryGetField<string>(header_index, out string header_name);

                    if (has_header)
                    {
                        header_index += 1;
                        csv_headers.Add(header_name);

                    }

                }

                Console.WriteLine(csv_headers.Count);

            }
        }
}
Paul Wild :

The header record is on the csv context. It needs to be read beforehand before accessing.

csv.Read();
csv.ReadHeader();
string[] headerRow = csv.Context.HeaderRecord;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a way to get a list of column names in sqlite?

What is the best way to copy a list?

What is the best way of using Arrays.asList() to initialize a List

What is the best way to get a derived status column based on existing result

What is the best way to configure multiple kafka topic names using KafkaProperties

What is the best way to refresh a list using Firebase

What is the best way to get the percentage for an object in linq list and map it to JSON?

"best" way to get size of a list

What is the best way to parameterize a pytest function using a fixture that returns a list?

What the best possible way to concatenate pandas column? From a list of column

What is the best way to both drop columns and insert column names using Python Pandas?

What is the best way to center list items using bootstrap?

What is the best way to trim a list?

What's the best way to organize a display list using AS3?

What's the best way to make matrix column names of the form [Q1, Q2, ...] in R?

What is the best way to set the followers list using parse.com

What is the best way to get the following data into long format using column labels?

What is the best way to Convert Degrees to Quadrant Names

What is the best way to categorize/group and sort a list of elements using javascript?

What's the best way to get the next 7 day names in node?

What's the best way to convert a date in the form yyyy-mm-dd to a date using English month names?

What is the best way to get a permutation index list of a given vector

What is the best way to print position of items in the list of strings using python

What is the best way to get one value from Column & row?

What's the best way to work with datasets that contain special characters in their column names in R?

What is the best way to get the absolute link of an <a> tag using JavaScript/jQuery?

Best way to get element in list

What is the fastest\best way to get file names from a folder using PowerShell?

What is the best way to get BSD drive names on macOS (Swift)?