Skip to content

C# DataTable #169

Answered by MarkPflug
ejbiker93ss asked this question in Q&A
Nov 25, 2022 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

Loading a data table could be as simple as:

var reader = CsvDataReader.Create("mydata.csv");
var dt = new DataTable();
dt.Load(reader);

However, loading the data this way will result in everything being interpreted as a string, which is likely not what you want. You can provide a schema for the data if you know what you're processing ahead of time.

This uses the Sylvan.Data package which provides the Schema type.

var schema =
    new Schema.Builder()
    .Add<string>("FirstName")
    .Add<string>("LastName")
    .Add<DateTime>("DateOfBirth")
    .Add<decimal>("Height")
    .Build();

var csvOptions = new CsvDataReaderOptions
{
    Schema = new CsvSchema(schema)
};

var reader = CsvDataRe…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@ejbiker93ss
Comment options

Answer selected by ejbiker93ss
Comment options

You must be logged in to vote
2 replies
@MarkPflug
Comment options

@ejbiker93ss
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants