Friday, November 3, 2017

I want to use LinqToExcel to scrape an Excel sheet's rows to a list of C# POCOs but I don't want all of the getsetters to be strings necessarily.

If you follow my suggestion here you just end up with strings and that is a pretty good place to start. Do not expect to just replace a string with an int or a nullable int as you'll end up with an error like this:

Object of type 'System.String' cannot be converted to type 'System.Int32'.

 
 

What is more, something like a string-to-become-a-DateTime may be in a funny shape in your Excel data and may need to be hit with hammers a little bit to give it the tough love it needs to become a DateTime. I looked into putting getsetters into my POCO that have extra mechanics in them to potentially cast strings to properties of other types on the same object, but that both breaks with the reflection magic I've rolled so far (which expects auto-properties) and just makes a dirty mess. When it is time to write records back to a database, you probably are going to use a different POCO suitable for NHibernate or the code first Entity Framework paradigm or the .edmx Entity Framework paradigm anyways so I would suggest just making a static method in a static utility class somewhere to just map the records complete with all of the casting logic and then get your ETL massage masseuse under unit tests.

No comments:

Post a Comment