Comments (3)
Looks like there is a bug in the Sylvan.Data binder when dealing with the new DateOnly/TimeOnly types. I've got a fix for that ready, and will push it shortly as 0.2.9.
However, there will also be a modification that needs to be made to your code. The issue is that the CsvDataReader doesn't "know" that any of the columns are a specific type. There are two ways to fix this.
One option is to specifically tell the CsvDataReader the types of the columns, by providing a "schema". Here is some test code demonstrating how to define the schema:
var data = "Name,Date\na,\"24/12/2022\"\nb,\"25/12/2022\"\n";
var schema =
new Schema.Builder()
.Add<string>("Name")
.Add<DateOnly>("Date")
// etc
.Build();
var opts = new CsvDataReaderOptions
{
DateOnlyFormat = "dd/MM/yyyy",
Schema = new CsvSchema(schema)
};
using var csv = CsvDataReader.Create(new StringReader(data), opts);
var records = csv.GetRecords<DateOnlyRecord>().ToList();
Assert.Equal(new DateOnly(2022, 12, 24), records[0].Date);
Assert.Equal(new DateOnly(2022, 12, 25), records[1].Date);
The other option is to tell the databinder to use the type of the property being bound to identify the types of the columns in the source data. The GetRecords<T>
method doesn't enable this by default, but it is easy to create your own GetRecords extension method that does:
public static IEnumerable<T> GetRecords<T>(this DbDataReader data)
where T : class, new()
{
var opts = new DataBinderOptions {
// tells the binder to use the member type
// to determine the appropriate data accessor
InferColumnTypeFromMember = true
};
var binder = DataBinder.Create<T>(data, opts);
while (data.Read())
{
var record = new T();
binder.Bind(data, record);
yield return record;
}
}
I'll post an update when the package is published.
from sylvan.
Okay, 0.2.9 has been published. It might take a few minutes to show up on nuget.
from sylvan.
Many thanks for the reply, that makes perfect sense
from sylvan.
Related Issues (20)
- Doc error HOT 2
- Ordering of columns HOT 3
- Nullable string from an empty string HOT 2
- Write headers when no data? HOT 1
- Cannot parse a CR delimited file HOT 2
- Non-descript exception thrown for Sylvan.Data.Csv HOT 3
- Processing files with large numbers of errors is rather slow HOT 3
- Query : Usage of Sylvan Libraries HOT 2
- Csv headers with "" HOT 3
- Skipping over rows that throw CsvFormatException HOT 2
- passing an IAsyncEnumerable to ObjectDataReader.Builder HOT 2
- As is. The Object to CSV Writer example doesn't compile HOT 1
- Clone a CsvDataReader HOT 1
- CsvDataWriter to append to a file HOT 1
- Configuration adjustments for CSV parsing HOT 1
- Records Count and Max Length of each Column HOT 1
- A 19kb record throws CsvRecordTooLargeException HOT 3
- Fields ending in escaped character return the wrong character HOT 1
- Minor unescape issue? HOT 2
- CompiledDataBinder: "Body of catch must have the same type as body of try" exception HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sylvan.