My question is different from other SO questions, because those deserialization samples are for a typed<Customer> POCO class
, in my scenario I don't have any types ahead of time. Files are being dropped in a folder from various sources.
Without, the type or header information, how would I deserialize/read a CSV file with unknown headers to a List<>, JSON Array or Enumerable (I don't need a datatable)
using System;
using System.Data;
using System.Collections;
using System.Linq;
using ChoETL;
using System.Text;
public class Program
{
public static void Main()
{
//headers are not known in the SourceFile.csv - i need a list and headers
//var csvLisFromCho = new ChoCSVReader(@"SourceFile.csv").WithFirstLineHeader();
StringBuilder csvIn = new StringBuilder(@"ID,Name
1, David
2, Bob");
using (var r = new ChoCSVReader(csvIn).WithFirstLineHeader())
{
var list = r.Select(r1 => r1.Values).ToList();
Console.WriteLine("Coverted List from Raj");
list.ForEach(Console.WriteLine);
}
}
}
Here you go, to get the list from CSV
StringBuilder csvIn = new StringBuilder(@"ID,Name
1, David
2, Bob");
using (var r = new ChoCSVReader(csvIn)
.WithFirstLineHeader()
)
{
var list = r.Select(r1 => r1.Values).ToList();
}
UPDATE:
To get the headers, cast record to IDictionary and use Keys property on it to get the keys.
In order to auto discover the datatypes of CSV columns, you must set the MaxScanRows on parser. Otherwise all columns will be treated as string type.
StringBuilder csvIn = new StringBuilder(@"ID,Name,Date
1, David, 1/1/2018
2, Bob, 2/12/2019");
using (var r = new ChoCSVReader(csvIn)
.WithFirstLineHeader()
.WithMaxScanRows(2)
)
{
foreach (IDictionary<string, object> rec in r.Take(1))
{
foreach (var kvp in rec)
Console.WriteLine($"{kvp.Key} - {r.Configuration[kvp.Key].FieldType}");
}
}