我需要使用 Dapper.net 将一对多扁平化 SQL 查询映射到嵌套对象中。
Slapper.Automapper 似乎是做到这一点的好方法;正如这个问题的答案中所详细说明的:
public string MrFlibble3()
{
using (var connection = new SqlConnection(Constr))
{
Slapper.AutoMapper.Cache.ClearInstanceCache();
const string sql = @"SELECT tc.[IDG] as ContactIdg
,tc.[ContactName] as ContactName
,tp.[Idg] AS TestPhones_PhoneIdg
,tp.[ContactIdg] AS TestPhones_ContactIdg
,tp.[Number] AS TestPhones_Number
FROM TestContact tc
INNER JOIN TestPhone tp ON tc.Idg = tp.ContactIdg";
// Step 1: Use Dapper to return the flat result as a Dynamic.
dynamic test = connection.Query<dynamic>(sql);
// Step 2: Use Slapper.Automapper for mapping …Run Code Online (Sandbox Code Playgroud)