c# - Dapper:具有重复列名称的多重映射

标签 c# dapper

我有一个如下所示的表格:

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

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

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();

关于c# - Dapper:具有重复列名称的多重映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996368/

相关文章:

c# - 如何优化这个 linq 查询?

c# - 反序列化不起作用

c# - WPF C# 应用程序中的 C++ Dll

c# - 启动新进程并杀死当前进程

c# - 如何在小巧玲珑中返回原始数据类型的元组

c# - html 表单发布到 web api 2 Controller

c# - 如何从 C# 代码与命令提示符进行交互?

Dapper - 带有字符串查询的表值参数

asp.net-core - NET Core 的 Dapper : Insert into a table and return id of inserted row

asp.net-mvc - 在不使用 EF 的情况下在 appsettings.json 中获取多个连接字符串