首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fluent映射- CsvHelper

Fluent映射- CsvHelper
EN

Stack Overflow用户
提问于 2015-09-16 00:32:57
回答 1查看 1.3K关注 0票数 0

我正在使用由乔希·克洛斯构建的CsvHelper库。我认为获取.csv文件的数据源有一种方法可以用正确的命名约定来掩蔽或“别名”列标题。不幸的是,情况并非如此,所以我想使用Fluent类映射,但我不清楚如何实现它。

我创建了下面的类(简化为这篇文章)

代码语言:javascript
复制
 public class PaymentType
{
    public int PaymentTypeId { get; set; }
    public string BusinessUnit { get; set; }
    public string Region { get; set; }
    public string Status { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public decimal Amount { get; set; }
    public string Name { get; set; }
}

下面的方法在另一个类中下载并保存该文件

代码语言:javascript
复制
private string DownloadDS(string getURL, string fileName)
    {
        try
        {
            //CSV path
            string url = getURL;

            //Where to save and retrieve CSV file
            string path = AppDomain.CurrentDomain.BaseDirectory + "ImportedFiles\\" + fileName;

            //Download file and save it 
            WebClient client = new WebClient();
            client.DownloadFile(url, path);



            return path;
        }
        catch
        {



            return "";
        }

    }

然后,此方法更新db。

代码语言:javascript
复制
 private void UpdateDB(string path, string fileName)
    {
        try
        {
            //read in file with CsvReader object and output to an enumerable object
            var csv = new CsvReader(new StreamReader(path));
            var importedPaymentTypes = csv.GetRecords<ImportPaymentTypes>();

            //Save each payment type record to the db.  If record exhsists, update it, if not, add it.


           ...

        }
        catch
        {
            ...


        }

    }

为了缩短代码片段,我去掉了一些日志记录和db逻辑。我读过关于Fluent映射的文章,但我对如何实现它感到困惑?我知道我必须构建一个类,但是如何引用/配置mapper类的使用呢?

以下是乔希网站的一个例子;

http://joshclose.github.io/CsvHelper/#reading-reading-all-records

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-16 14:47:42

请看一下映射部分。http://joshclose.github.io/CsvHelper/#mapping

通过以下操作注册映射:

代码语言:javascript
复制
csv.Configuration.RegisterClassMap<PaymentTypeMap>();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32597840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档