CsvHelper 从两种混合排序的类型中写入 header

CsvHelper writing header from two types with mixed ordering

我正在使用 CsvHelper 将两个 classes 的属性写入 CSV。我有他们两个的地图。问题是我希望 header/fields 的顺序是

filename, FooId, BarId, FooName, BarName
file.txt, f1, b1, myFoo, myBar

它只显示为

filename, FooId, FooName, BarId, BarName
file.txt, f1, myFoo, b1, myBar

这是一些相关代码

public class Foo {
    public string Filename { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
    
}

public class FooMap : ClassMap<Foo> {
    public FooMap() {
        Map(m => m.Filename).Index(0).Name("filename");
        Map(m => m.Id).Index(1).Name("FooId");
        Map(m => m.Name).Index(3).Name("FooName");
    }
}

public class Bar {
    public string Id { get; set; }
    public string Name { get; set; }
}

public class BarMap : ClassMap<Bar> {
    public BarMap() {
        Map(m => m.Id).Index(2).Name("BarId");
        Map(m => m.Name).Index(4).Name("BarName");
    }
}

public static void Main() {
    Foo foo1 = new("file.txt", "f1", "myFoo");
    Bar bar1 = new("b1", "myBar");
    using (var reader = new StreamReader("Path/to/my/csv"))
    using (var csv = new CsvWriter(reader, CultureInfo.InvariantCulture))
    {
        csv.Context.RegisterClassMap<FooMap>();
        csv.Context.RegisterClassMap<BarMap>();

        csv.WriteHeader<Foo>();
        csv.WriteHeader<Bar>();
        csv.NextRecord();

        while (csv.Read())
        {
             csv.WriteRecord(foo);
             csv.WriteRecord(bar);
             csv.NextRecord();
        }
}

class 的构造函数是基本的。如果你也想让我 post 它,我会的!我的主要问题是是否可以在映射 中混合使用两个 classes ,而不是手动写出每个字段。

您可以将它们组合成一个 FooBar class,然后映射 FooBar class.

void Main()
{
    var records = new List<FooBar> 
    {
        new FooBar 
        {           
            Foo = new Foo { FileName = "file.txt", Id = "fi", Name = "myFoo"},
            Bar = new Bar { Id = "bi", Name = "myBar" }
        }
    };
    
    using (var csv = new CsvWriter(Console.Out, CultureInfo.InvariantCulture))
    {  
        csv.Context.RegisterClassMap<FooBarMap>();
        csv.WriteRecords(records);
    }
}

public class FooBar
{
    public Foo Foo { get; set; }
    public Bar Bar { get; set; }
}

public class FooBarMap : ClassMap<FooBar>
{
    public FooBarMap()
    {
        Map(x => x.Foo.FileName).Name("filename").Index(0);
        Map(x => x.Foo.Id).Name("FooId").Index(1);
        Map(x => x.Foo.Name).Name("FooName").Index(3);
        Map(x => x.Bar.Id).Name("BarId").Index(2);
        Map(x => x.Bar.Name).Name("BarName").Index(4);
    }
}

public class Foo
{
    public string FileName { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
}

public class Bar
{
    public string Id { get; set; }
    public string Name { get; set; }
}