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

时间:2015-08-13 19:00:32

标签: 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
}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

以下是:

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