c# - CsvHelper ignore not working -


I csvhelper to generate a CSV file based on the list CsvClassMap to specify the area that I want to ignore, however, the value is still Also being written in the file.

Here's my class:

  public class person {public Ent ID (Receive); Set; } Public string first name {received; Set; } Public string middlename {get; Set; } Public String LastName {get; Set; }}  

Here is my CsvClassMap :

  Public Sealed Class PersonClassMap: CsvClassMap & lt; Person & gt; {PUBLIC CLASSSP () {MAP (M = & gt; MIID) Index (0) .name ("id"); Map (M = & gt; m.FirstName) Index (1) .name ("first name"); Map (M = & gt; m.LastName) Index (2) .name ("last name"); Map (M = & gt; m.MiddleName). rare (); }}  

and this is the code that I am using to generate output:

  var person = new list & lt; Person & gt; {New person {id = 1, first name = "Randall", Middle name = "Michael", LastName = "Perry"}, new person {id = 2, first name = "Marigold", MiddleName = "Joan", LastName = "Mercier"}, new person {id = 3, first name = "sven", middle name = "ergenfin", lastname = "olafusen"}}; (Var csvWriter = New CsvWriter (textWriter)) {csvWriter.WriteRecords (individuals); TextWriter.Flush (); } My output is as follows:  
 id, first name, middle name, lastname 1, randall How can I stop writing this  MiddleName  

The map of the square should be in order on the runtime to find out how to use it:

  to use (var csvWriter = new csvwriter (TextWriter)) {CsvWriter.configuration.Regist ErClassMap & lt; PersonClassMap & gt; (); CsvWriter.WriteRecords (person); TextWriter.Flush (); }  

Also keep in mind, in the current version, you will be in the class map (though it will change in the future):

Meditation Do not give

Currently it is not used. Mapping will only map the properties you specify. In the future, there will be an option to auto map within a map, and explicitly stated that any mapping will override the auto-mapped people. When this happens, to ignore it will be used to ignore a property that was automatically mapped.

With this in mind, you can also simplify the map of your class in this way:

  Public Sealed Class PersonClassMap: CsvClassMap & lt; Person & gt; {PUBLIC CLASSSP () {MAP (M = & gt; MIID) Index (0) .name ("id"); Map (M = & gt; m.FirstName) Index (1) .name ("first name"); Map (M = & gt; m.LastName) Index (2) .name ("last name"); }}  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -