Dapper:具有重复列名的多重映射 [英] Dapper: Multi-Mapping with repeating column names

查看:191
本文介绍了Dapper:具有重复列名的多重映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的表:

I have a table that looks like this:

ID   ERR1 ERR2 ERR3
---- ---- ---- ----
05A2 A001 B223 C212
06B3 B392 C234 D234
...

我想将其映射到如下所示的对象:

I would like to map it to objects that look like this:

public class Entry
{
    public string Id { get; set; }
    public List<BpcError> Errors { get; set; }

    public Entry()
    {
        Errors = new List<BpcError>();
    }
}

public class BpcError
{
    public string ErrorCode { get; set; }
    // More properties and methods to follow
}

我该如何

推荐答案

方法如下:

string sql = "SELECT ID, "
    + "ERR1 AS ErrorCode, "
    + "ERR2 AS ErrorCode, "
    + "ERR3 AS ErrorCode "
    + "FROM ERR_TB";

List<Entry> entries = connection.Query<Entry, BpcError, BpcError, BpcError, Entry>(sql,
(entry, e1, e2, e3) =>
{
    if (e1 != null)
        entry.Errors.Add(e1);

    if (e2 != null)
        entry.Errors.Add(e2);

    if (e3 != null)
        entry.Errors.Add(e3);

    return entry;
},
splitOn: "ErrorCode, ErrorCode, ErrorCode")
.ToList();

这篇关于Dapper:具有重复列名的多重映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆