Csvhelper: Write Variable Parent Name before all Child properties

Created on 17 Jul 2018  路  2Comments  路  Source: JoshClose/CsvHelper

Hi,

I have a Garage object that I want to write to a csv file.

Ex:

public class Garage{
    public Car Car1 {get; set;}
    public Car Car2 {get; set;}
}
public class Car{
    public string Model {get; set;}
    public string Manufacturer {get; set;}
    public int Year {get; set;}
}

I would like that the csv column of a Garage would look like this:
Car1_Model, Car1_Manufacturer, Car1_Year, Car2_Model, Car2_Manufacturer, Car2_Year

In general: [NameOfParentProperty]_[NameOfChildProperty]

With or without underscores is fine...

At the time I have to make another object called GarageResult:

public class GarageResult{
    public string Car1_Model {get; set;}
    public string Car1_Manufacturer {get; set;}
    public string Car1_Year {get; set;}
    public string Car2_Model {get; set;}
    public string Car2_Manufacturer {get; set;}
    public string Car2_Year {get; set;}

    public GarageResult(Car car1, Car car2){
        this.Car1_Model = car1.Model;
        this.Car1_Manufacturer = car1.Manufacturer;
        this.Car1_Year = car1.Year;
        this.Car2_Model = car2.Model;
        this.Car2_Manufacturer = car2.Manufacturer;
        this.Car2_Year = car2.Year;
}

But each time I want to add a Car to Garage, I need to adjust the class GarageResult accordingly and the constructor...

Do you have any solutions for me to avoid the class GarageResult and be able to just WriteRecords an Enumerable of Garage ?

Most helpful comment

You should be able to do this by setting ReferenceHeaderPrefix:

var testData = new List<Garage>
{
    new Garage {Car1 = new Car { Model = "Escort", Manufacturer = "Ford", Year = 2017 }, Car2 = new Car { Model = "Yaris", Manufacturer = "Toyota", Year = 2018 } }
};

using (var writer = new StreamWriter("test.csv"))
{
    var csv = new CsvWriter(writer);
    csv.Configuration.ReferenceHeaderPrefix = (memberType, memberName) => $"{memberName}_";
    csv.WriteRecords<Garage>(testData);
}

I think the documentation for this feature might not have been updated since ea51cde8d98ef0e8c2d01b3c49d5e816afb38454 - as it still mentions the old PrefixReferenceHeaders (which has been removed)

All 2 comments

You should be able to do this by setting ReferenceHeaderPrefix:

var testData = new List<Garage>
{
    new Garage {Car1 = new Car { Model = "Escort", Manufacturer = "Ford", Year = 2017 }, Car2 = new Car { Model = "Yaris", Manufacturer = "Toyota", Year = 2018 } }
};

using (var writer = new StreamWriter("test.csv"))
{
    var csv = new CsvWriter(writer);
    csv.Configuration.ReferenceHeaderPrefix = (memberType, memberName) => $"{memberName}_";
    csv.WriteRecords<Garage>(testData);
}

I think the documentation for this feature might not have been updated since ea51cde8d98ef0e8c2d01b3c49d5e816afb38454 - as it still mentions the old PrefixReferenceHeaders (which has been removed)

@FireEater64 Thank you so much ! Exactly what I was looking for ! Making the code so much cleaner ! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mabead picture mabead  路  3Comments

RizwanAhmedJutt picture RizwanAhmedJutt  路  5Comments

RifS picture RifS  路  5Comments

NeilMeredith picture NeilMeredith  路  4Comments

muzzamo picture muzzamo  路  5Comments