Csvhelper: Ignore Single Property without Creating ClassMap

Created on 24 Jun 2014  路  20Comments  路  Source: JoshClose/CsvHelper

I am mapping from a csv file into a database using this library. (I know there are other ways to do this, but this is a great flexible library for this.) My classes mirror the csv files exactly except a single property, the property that maps to the Identity column in the database. It would be great to ignore this single property on read using an attribute set on the property.

CSV file structure:

Column Name, Type
Name, string
Email, string
Phone, string

Class:

``` c#
public class Person {
public int PersonId {get;set;}
public string Name {get;set;}
public string Email {get;set;}
public string Phone {get;set;}
}

Database Table:

``` sql
create table Person (
PersonId int identity primary key not null,
Name nvarchar(50) null,
Email nvarchar(50) null,
Phone nvarchar(50) null
)

(note: this method works currently, but it seems like overkill to ignore a single property https://github.com/JoshClose/CsvHelper/issues/236 )

Most helpful comment

I really really really do not want to add attributes back in. :) I thought about adding just [Ignore], but I don't want to even do that now...

You are able to do this now though.

c# public sealed MyClassMap : CsvClassMap<MyClass> { public MyClassMap() { AutoMap(); Map( m => m.MyProperty ).Ignore(); } }

Automap the class and make any alterations after.

All 20 comments

People have been asking for attribute mapping to come back. If it does, it will be in a limited fashion. https://github.com/JoshClose/CsvHelper/pull/277

Perhaps by default reading from a flat file does not throw an error if there is an extra property in the class? Map columns from the flat file that have matching properties, don't map/ignore columns that do not have matching properties, and don't throw errors if there are extra properties?

I really really really do not want to add attributes back in. :) I thought about adding just [Ignore], but I don't want to even do that now...

You are able to do this now though.

c# public sealed MyClassMap : CsvClassMap<MyClass> { public MyClassMap() { AutoMap(); Map( m => m.MyProperty ).Ignore(); } }

Automap the class and make any alterations after.

Hey Josh!

First off, great library! Thank you!

I have a requirement where I need to remove specified columns from csv鈥檚. The number of csv鈥檚 and the data structure can vary greatly. I am currently using GetRecords to read in the raw data. Is there an easy way if I have the name of the property at run time to ignore or remove the property or ignore it when writing to a new csv? I was hoping to avoid having to create classes to represent all of the disparate csv鈥檚. I know I could use ClassMaps and the ignore functionality if I can鈥檛 find a way to handle this dynamically at run time. If you鈥檙e willing to share your thoughts I鈥檇 really appreciate it!

Thanks!

Using dynamic to read in the raw data.

What do you mean by using dynamic? GetRecords<dynamic>()?

Also, can you create a new issue so it doesn't get lost on this closed issue? Thanks.

@JoshClose may i use this same above thing with the CSVHelper version 2.13.2 , i need to stick with this version .

Latest Version supports this :

public sealed MyClassMap : CsvClassMap
{
public MyClassMap()
{
AutoMap();
Map( m => m.MyProperty ).Ignore();
}
}

how to achieve the above same thing with the CSVHelper version 2.13.2

-Thanks

Yes. Here is an example using 2.13.2.

void Main()
{
    var s = new StringBuilder();
    s.Append("Id,Name\r\n");
    s.Append("1,one\r\n");
    s.Append("2,two\r\n");
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader))
    {       
        csv.Configuration.RegisterClassMap<TestMap>();
        csv.GetRecords<Test>().ToList().Dump();
    }
}

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Extra { get; set; }
}

public class TestMap : CsvClassMap<Test>
{       
    public TestMap()
    {
        AutoMap();
        Map(m => m.Extra).Ignore();
    }
}

@JoshClose how to make class TestMap generic ( ) to accept the diff. dto's classes .

I don't understand what you're asking.

@JoshClose
I have posted here , just needs to avoid the writing multiple TestMap class ,wants generic class
https://stackoverflow.com/questions/51297834/how-to-make-class-generic-to-pass-dynamic-property-class-in-c-sharp

Answered there.

Yes , Thank You .

@JoshClose i need another help , everything is working fine but after writing csv file columns index is changed , i have dto class like this for ex. ```

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Extra { get; set; }
}

but after writing in csv column index changes , is there any common way to do maintain indexing for class like test , bcz i am working with too many dto classes

You mean the order that the columns are written? You need to specify the index in a class map or an attribute.

yes column order , i want to void indexing in map class , is there any way to do that ?

No. .NET doesn't guarantee the order of properties. You can put an [Index(1)] attribute on the property if you don't want to do it in the map.

Thanks @JoshClose might be i need to work with indexing in map class, but not understood the this part [Index(1)] attribute on the property can you share any example.

public class Test
{
    [Index(0)]
    public int Id { get; set; }
    [Index(1)]
    public string Name { get; set; }
    [Index(2)]
    public decimal Extra { get; set; }
}

Not working for me i am using 4.1 framework in visual studio.
Thanks .

Was this page helpful?
0 / 5 - 0 ratings