csvhelper

Write an object with a List<T> field to CSV

青春壹個敷衍的年華 提交于 2020-01-25 08:35:08
问题 I'm learning C# and object serialization. I need to save an object in a .csv file. I tried CsvHelper but couldn't find the answer to my question. Suppose I have an object like this: public class Record { ... public List<string> Collection { get; set; } ... } How do I use CsvHelper to store the values of Collection in a .csv file along with other primitive types in a record of csv file? 回答1: Collections are ignored by CsvHelper by default. However, using a ClassMap you can use Index to

Write an object with a List<T> field to CSV

落花浮王杯 提交于 2020-01-25 08:35:05
问题 I'm learning C# and object serialization. I need to save an object in a .csv file. I tried CsvHelper but couldn't find the answer to my question. Suppose I have an object like this: public class Record { ... public List<string> Collection { get; set; } ... } How do I use CsvHelper to store the values of Collection in a .csv file along with other primitive types in a record of csv file? 回答1: Collections are ignored by CsvHelper by default. However, using a ClassMap you can use Index to

Merging CSV files with different headers using CSVhelper C#

拥有回忆 提交于 2020-01-14 12:57:05
问题 When trying to merge multiple .csv files from a directory into one .csv file using CSVhelper. In the directory there are 50 .csv files, among these 50 files there are two sets of file structures, one with 7 columns and one with 6. Every file has the exact same first 5 headers however depending on the file the last two columns will change. Example of CSVfile format 1: Example of CSVfile format 2: Every file in the directory will hold either of these structures with different data in the

How to add CsvHelper records to DataTable to use for SqlBulkCopy to the database

谁说胖子不能爱 提交于 2020-01-12 07:06:31
问题 I am trying to read a CSV file with CsvHelper, load each record into a DataTable, and then use SqlBulkCopy to insert the data into a database table. With the current code, I get an exception when adding a row to the DataTable. The exception is: "Unable to cast object of type 'MvcStockAnalysis.Models.StockPrice' to type 'System.IConvertible'.Couldn't store in Date Column. Expected type is DateTime." The example CSV file is from yahoo finance. For example: http://ichart.yahoo.com/table.csv?s

CSVHelper - using two mapping classes and choosing the mapping based on the value in a field of each row of the csv file

安稳与你 提交于 2020-01-06 08:14:46
问题 Using the CSVHelper .NET library with .NET Core 2.2 to parse large csv file (over 1 million rows) and write it to a SQL Server table. I have two mapping classes: we need to loop through each row, and if the first value of the row is 1, we need to use class map 1, and if the value is 2, we need to use class map 2. Because CSVHelper is geared toward doing this activity in bulk, I'm having trouble conceptualizing how to use the if statement and for each loop to accomplish this task. This what I

DateTime Formatting on Azure hosted site

人盡茶涼 提交于 2020-01-04 05:44:11
问题 so my tech stack is: asp.net core 1.1 hosted on Azure using CSV Helper to read CSVs into a SQL database (in azure). ok so the problem i'm facing is when i'm using CSV helper locally its all fine, the date format is reading it as per the CSV file into the format i want (dd/mm/yyyy i'm in sydney). when i import a file hosted on the azure site.. my date formats are screwed (they are converting to the US). how do i go about importing those dates correctly using CSV Helper? in my startup.cs i have

Reading multiple classes from single csv file using CsvHelper

痴心易碎 提交于 2020-01-03 15:56:52
问题 I've been using Josh Close' CsvHelper a bit recently to parse CSV files, I quite like the fluent api for class mapping. I'm trying to map a csv file which contains multiple record types, the file structure is C,Comment,Timestamp I,Class1,Header1,Header2 D,Class1,Data1,Data2 D,Class1,Data1,Data2 ... I,Class2,Header1,Header2,Header3 D,Class2,Data1,Data2,Data3 D,Class2,Data1,Data2,Data3 ... C,Checksum Is this something which can be handled by CsvHelper? I've writen a custom parser which

Adding detectable Nullable values to CsvHelper

人盡茶涼 提交于 2020-01-02 01:56:08
问题 I was wondering if CsvHelper by Josh Close has anything in the configuration I am missing to translate values to null. I am a huge fan of this library, but I always thought there should be some sort of configuration to let it know what values represent NULL in your file. An example would be a column with the value "NA", "EMPTY", "NULL", etc. I am sure I could create my own TypeConverter, but I was hoping there would be an easier option to set somewhere in a config as this tends to be fairly

Using CSVHelper on file upload

不羁的心 提交于 2019-12-30 06:14:12
问题 I am trying to use the CSVhelper plugin to read an uploaded CSV file. Here is my modelBinder class: public class SurveyEmailListModelsModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var csv = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var file = ((csv.RawValue as HttpPostedFileBase[]) ?? Enumerable.Empty<HttpPostedFileBase>()).FirstOrDefault(); if (file == null || file

CsvHelper - Read different record types in same CSV

≯℡__Kan透↙ 提交于 2019-12-25 06:29:05
问题 I'm trying to read two types of records out of a CSV file with the following structure: PlaceName,Longitude,Latitude,Elevation NameString,123.456,56.78,40 Date,Count 1/1/2012,1 2/1/2012,3 3/1/2012,10 4/2/2012,6 I know this question has been covered previously in Reading multiple classes from single csv file using CsvHelper Multiple Record Types in One File? but when I run my implementation it gets a CsvMissingFieldException saying that Fields 'Date' do not exist in the CSV file . I have two