如何使用CsvClassMap将一个类的属性映射到具有不同名称的另一个类?

huangapple go评论72阅读模式
英文:

How to map one class properties to another using class with different names using CsvClassMap

问题

我的应用程序读取没有标题的 .CSV 文件,并将其转换为 XML 文件。
对于现有的代码,写成如下:

sr = new StreamReader(fs);
fs = null;

using (CsvReader csvReader = new CsvReader(sr))
{
    sr = null;

    csvReader.Configuration.HasHeaderRecord = hasHeaderRecord;
    csvReader.Configuration.IgnoreBlankLines = false;
    csvReader.Configuration.IgnoreReadingExceptions = true;
    csvReader.Configuration.WillThrowOnMissingField = false;
    csvReader.Configuration.TrimFields = true;

    csvReader.Configuration.RegisterClassMap<Class1Map>();
    FileRecords = csvReader.GetRecords<Class1>().ToList();
}

public class Class1Map : CsvClassMap<Class1>
{
    public Class1Map()
    {
        Map(m => m.AccountId).Index(0);
        Map(m => m.MeterId).Index(1);
        // 其他映射...
    }
}

但是现在对于我的新需求,.csv 文件包含标题和与之前的 .csv 不同的列名。不知何故,我必须读取新的 CSV 文件并获取其中的值,然后将其映射到 Class1。

Class1 的属性是 AccountId、MeterId 等,但在新格式中,名称已经不同了。AccountId 变为 AccountRef,MeterId 变为 MeterSerial。

有人能建议如何将新文件的 AccountRef、MeterSerial 值映射到 Class1 的属性 AccountId、MeterId 吗?

英文:

My application reads .CSV file(which do not having a header in csv file) and converts into XML file.
For existing code wrote as

sr = new StreamReader(fs);
fs = null;

using (CsvReader csvReader = new CsvReader(sr))
{
    sr = null;

    csvReader.Configuration.HasHeaderRecord = hasHeaderRecord;
    csvReader.Configuration.IgnoreBlankLines = false;
    csvReader.Configuration.IgnoreReadingExceptions = true;
    csvReader.Configuration.WillThrowOnMissingField = false;
    csvReader.Configuration.TrimFields = true;

    csvReader.Configuration.RegisterClassMap&lt;Class1Map&gt;();
    FileRecords = csvReader.GetRecords&lt;Class1&gt;().ToList();
}


public class Class1Map : CsvClassMap&lt;Class1&gt;
    {
        public Class1Map()
        {
            Map(m =&gt; m.AccountId).Index(0);
            Map(m =&gt; m.MeterId).Index(1);
            .......
            .......
        }
     }

But now for my new requirement, .csv file includes header and column names that are different compared to previous .csv. Somehow I have read the new CSV file and get values present in the csv file and mapped to class1.

Class1 properties are AccountId,MeterId etc.
But in new format the names are different now.
AccountId as AccountRef and MeterId as MeterSerial.

Can any one suggest how to map new file values of AccountRef,MeterSerial to class1 properties AccountId,MeterId

答案1

得分: 0

你可以在你的映射中添加 .Name()。在没有标题的第一个示例中,将使用 .Index(),而在有标题的第二个示例中,将使用 .Name() 来映射列。

英文:

You could just add .Name() to your maps. Your first example with no header will use .Index() and your second example with a header will use .Name() to map the columns.

void Main()
{
	var config1 = new CsvConfiguration(CultureInfo.InvariantCulture)
	{
		HasHeaderRecord = false
	};

	using (var reader = new StringReader(&quot;1,2\n3,4&quot;))
	using (var csv = new CsvReader(reader, config1))
	{
		csv.Context.RegisterClassMap&lt;Class1Map&gt;();
	    var records = csv.GetRecords&lt;Class1&gt;().Dump();
	}

	var config2 = new CsvConfiguration(CultureInfo.InvariantCulture)
	{
		HasHeaderRecord = true
	};

	using (var reader = new StringReader(&quot;MeterSerial,AccountRef\n4,5\n6,7&quot;))
	using (var csv = new CsvReader(reader, config2))
	{
		csv.Context.RegisterClassMap&lt;Class1Map&gt;();
		var records = csv.GetRecords&lt;Class1&gt;().Dump();
	}
}

public class Class1Map : ClassMap&lt;Class1&gt;
{
	public Class1Map()
	{
		Map(m =&gt; m.AccountId).Index(0).Name(&quot;AccountRef&quot;);
		Map(m =&gt; m.MeterId).Index(1).Name(&quot;MeterSerial&quot;);
	}
}

public class Class1
{
	public int AccountId { get; set; }
	public int MeterId { get; set; }
}

huangapple
  • 本文由 发表于 2023年1月6日 14:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定