CSVHelper - Import CSV data

Language:: CSharp Type:: Back-end Context:: Utility function for CSV

Description:: A generic method to get the typed data from a csv file

Snippet:

public class CsvHelper
    {
        public IEnumerable<T> GetRecords<T>(TextReader reader)
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                PrepareHeaderForMatch = args =>
                {
                    var emptyChar = "";
                    return args.Header.Replace(" ", emptyChar).Replace(".", emptyChar);
                },
                HeaderValidated = null,
                MissingFieldFound = null
            };
 
            using (var csv = new CsvReader(reader, config))
            {
                var records = csv.GetRecords<T>();
                return records.ToList<T>();
            }
 
        }
    }

Type::#type/snippet