我正在使用 Dapper multimap 功能并将值映射到各种模型。我正在努力解决如何将映射的值分配给对象的问题。我知道 lambda 函数正在循环遍历行,并且我能够看到简洁对象中的值,但是当我去分配值时,我无法从中获取值:
var certificationReviews = await connection.QueryAsync<ARMS_API.Models.CertificationReview>("select * from dbo.A2F_DMCP_0005_BrowseTest()",
types: new[] {
typeof(ARMS_API.Models.CertificationReview),
typeof(State),
typeof(ARMS_API.Models.Program),
typeof(ActuarialFirm),
typeof(object),
typeof(object),
typeof(object) ,
typeof(object)
}, map: (objects) => {
var certificationReview = (ARMS_API.Models.CertificationReview)objects[0];
certificationReview.STATE = (State)objects[1];
certificationReview.STATE.PROGRAMS.Add((ARMS_API.Models.Program)objects[2]);
certificationReview.ACTUARIAL_FIRM = (ActuarialFirm)objects[3];
certificationReview.ACTUARIAL_FIRM.CERTIFYING_ACTUARY.Add(
new User {
INTERNAL_USER_NUMBER = (int?)(object)objects[4].GetType().GetProperty("CERTIFYING_ACTUARY_ID"),
FIRST_NAME = (string?)(Object)objects[4].GetType().GetProperty("CERTIFYING_ACTUARY_FIRST_NAME"),
LAST_NAME = (string?)(Object)objects[4].GetType().GetProperty("CERTIFYING_ACTUARY_LAST_NAME")
});
certificationReview.PRIMARY_REVIEWER =
new User {
INTERNAL_USER_NUMBER = (int?)(object)objects[4].GetType().GetProperty("PRIMARY_ACTUARY_ID"),
FIRST_NAME = (string?)(Object)objects[4].GetType().GetProperty("PRIMARY_ACTUARY_FIRST_NAME"),
LAST_NAME = (string?)(Object)objects[4].GetType().GetProperty("PRIMARY_ACTUARY_LAST_NAME")
};
certificationReview.SECONDARY_REVIEWER =
new User {
INTERNAL_USER_NUMBER = (int?)(object)objects[4].GetType().GetProperty("SECONDARY_ACTUARY_ID"),
FIRST_NAME = (string?)(Object)objects[4].GetType().GetProperty("SECONDARY_ACTUARY_FIRST_NAME"),
LAST_NAME = (string?)(Object)objects[4].GetType().GetProperty("SECONDARY_ACTUARY_LAST_NAME")
};
certificationReview.DMCP_ANALYST =
new User {
INTERNAL_USER_NUMBER = (int?)(object)objects[4].GetType().GetProperty("DMCP_ANALYST_ID"),
FIRST_NAME = (string?)(Object)objects[4].GetType().GetProperty("DMCP_ANALYST_FIRST_NAME"),
LAST_NAME = (string?)(Object)objects[4].GetType().GetProperty("DMCP_ANALYST_LAST_NAME")
};
return certificationReview;
},
splitOn: "STATE_ID,PROGRAM_DD,ACTUARIAL_FIRM_DD,CERTIFYING_ACTUARY_ID,PRIMARY_ACTUARY_ID,SECONDARY_ACTUARY_ID,DMCP_ANALYST_ID");
return (IEnumerable<CertificationReview>)certificationReviews;
}
}
当代码到达时就对了
(int?)(object)objects[4].GetType().GetProperty("CERTIFYING_ACTUARY_ID"),
我将鼠标悬停在对象[4]上,我可以看到该对象包含我尝试从中提取的所有字段,例如“CERTIFYING_ACTUARY_ID”,但我不知道如何提取该值。我尝试了 .GetType().GetProperty("CERTIFYING_ACTUARY_ID") 如here所示,但是除了添加 GetValue 等之外什么也没做,而且它们都没有提取我在对象中看到的值,因此结果为 null。我还必须进行显式类型转换,否则会出现空错误。不知道如何获取值!
您并没有利用 Dapper 的映射功能,而是试图将一些复杂、难以维护的代码混合到其中。使用动态代替对象,让 Dapper 完成映射工作。解决方案是这样的:
string sql = "select * from dbo.A2F_DMCP_0005_BrowseTest()";
var certificationReviews = await connection.QueryAsync<ARMS_API.Models.CertificationReview, State, ARMS_API.Models.Program, ActuarialFirm, dynamic, dynamic, dynamic, dynamic>(sql,
map: (certificationReview, state, program, firm, user1, user2, user3, user4) => {
certificationReview.STATE = state;
certificationReview.STATE.PROGRAMS.Add(program);
certificationReview.ACTUARIAL_FIRM = firm;
certificationReview.ACTUARIAL_FIRM.CERTIFYING_ACTUARY.Add(
new User {
INTERNAL_USER_NUMBER = user1.CERTIFYING_ACTUARY_ID,
FIRST_NAME = user1.CERTIFYING_ACTUARY_FIRST_NAME,
LAST_NAME = user1.CERTIFYING_ACTUARY_LAST_NAME
});
certificationReview.PRIMARY_REVIEWER =
new User {
INTERNAL_USER_NUMBER = user2.PRIMARY_ACTUARY_ID,
FIRST_NAME = user2.PRIMARY_ACTUARY_FIRST_NAME,
LAST_NAME = user2.PRIMARY_ACTUARY_LAST_NAME
};
certificationReview.SECONDARY_REVIEWER =
new User {
INTERNAL_USER_NUMBER = user3.SECONDARY_ACTUARY_ID,
FIRST_NAME = user3.SECONDARY_ACTUARY_FIRST_NAME,
LAST_NAME = user3.SECONDARY_ACTUARY_LAST_NAME
};
certificationReview.DMCP_ANALYST =
new User {
INTERNAL_USER_NUMBER = user4.DMCP_ANALYST_ID,
FIRST_NAME = user4.DMCP_ANALYST_FIRST_NAME,
LAST_NAME = user4.DMCP_ANALYST_LAST_NAME
};
return certificationReview;
},
splitOn: "STATE_ID,PROGRAM_DD,ACTUARIAL_FIRM_DD,CERTIFYING_ACTUARY_ID,PRIMARY_ACTUARY_ID,SECONDARY_ACTUARY_ID,DMCP_ANALYST_ID"
);
return (IEnumerable<CertificationReview>)certificationReviews;
}
}
这可能不会按您期望的方式工作,您只会在您调用
.Add
的地方获得一件物品。如果这不是您想要的,您需要查看在 Dapper 中使用连接的技术。
我更喜欢一个没有动态的解决方案,将每个用户映射到它自己的类,扩展User
。