Dapper:具有重复列名的多映射
Dapper: Multi-Mapping with repeating column names
我有一个 table 看起来像这样:
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();
我有一个 table 看起来像这样:
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();