Comments (5)
You should be able to mix as needed. I wouldn't necessarily recommend that approach, but it should work.
from sylvan.
The "databinder" doesn't know about formats, that's the responsibility of the CsvDataReader. You can set the date format on the CsvDataReaderOptions parameter. Here is anexample test:
var data = "d,x\n01/02/03,X";
var opts = new CsvDataReaderOptions { DateTimeFormat = "dd/MM/yy" };
var csv = CsvDataReader.Create(new StringReader(data), opts);
Assert.True(csv.Read());
Assert.Equal(new DateTime(2003, 2, 1), csv.GetDateTime(0));
from sylvan.
Thanks Mark.. Til now I'd just used a hack of binding to a string property and having the setter do the parse into an ignored DateTime field, because there were actually two different date formats in the file (sigh). I take it there's no scope for supporting that with a CSVDR as-is? i.e. are we currently stuck with the date format being a file-level config rather than column-level configurable?
from sylvan.
No, I support that too. You can set the format on a per-column basis via the "Schema" property. The schema is essentially a collection of DbColumn
(bcl type) that specifies information about the column. I've extended the base type to support a "format" custom value. You can also "remap" the column names with the schema, to avoid putting attributes on your data record types. Here's an example:
[Fact]
public void DateFormat()
{
var data = "a,b\n01/02/03,01/02/03";
var schema = Schema.Parse("a>Something:DateTime{MM/dd/yy},b>Other:DateTime{dd/MM/yy}");
var opts = new CsvDataReaderOptions {
Schema = new CsvSchema(schema) };
var csv = CsvDataReader.Create(new StringReader(data), opts);
var records = csv.GetRecords<TestRecord>().ToList();
Assert.Equal(new DateTime(2003, 1, 2), records[0].Something);
Assert.Equal(new DateTime(2003, 2, 1), records[0].Other);
}
class TestRecord
{
public DateTime Something { get; set; }
public DateTime Other { get; set; }
}
from sylvan.
Does a Schema have to completely specify the entire mapping for a file, or can we mix DataMember attribs in with schema specifiers?
from sylvan.
Related Issues (20)
- 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
- Bulk Load CSV data into Postgres? HOT 1
- DbDataReader reads all rows as empty from IEnumerable<object> HOT 5
- The ObjectDataReader should use DataMember attribute to allow column name mapping HOT 1
- Encoding problem HOT 3
- DataTable Columns and Rows HOT 3
- Reading many csv files, checking the length of fields and merging them all into one file HOT 3
- CsvWriter to output to a .gz file HOT 1
- Invalid Examples for CSV Data Binding HOT 2
- CSV: RowNumber and line breaks inside fields HOT 1
- nullables issue, how to get the property names, and type info HOT 1
- Query: Nullable boolean CsvDataWriterOptions FalseString HOT 4
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.