首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >dapper -multi-mapping:平面sql返回到嵌套对象

dapper -multi-mapping:平面sql返回到嵌套对象
EN

Stack Overflow用户
提问于 2011-08-16 20:52:30
回答 1查看 19.6K关注 0票数 20

我有一个包含address对象的公司。SQL返回的是扁平的,我正在尝试让Query<>加载所有的对象。

代码语言:javascript
复制
cnn.Query<Company,Mailing,Physical,Company>("Sproc", 
                    (org,mail,phy) =>
                    {
                        org.Mailing = mail;
                        org.Physical = phy;
                        return org;
                    },
                    new { ListOfPartyId = stringList }, null, true, commandTimeout: null,
                    commandType: CommandType.StoredProcedure, splitOn: "MailingId,PhyscialId").ToList();

我也不确定我的SplitOn是否正确。我明白了:

使用多映射API时,如果您的密钥不是Id参数名称: splitOn,请确保设置了API参数

建议会很棒的。

Test.cs中的示例并不是代码要求的查询参数。这些需要更新

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-17 17:29:06

对我来说这是完美的。也许是打字错误?

我看到了PhyscialId,它看起来确实很像。

代码语言:javascript
复制
class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Mailing Mailing { get; set; }
    public Physical Physical { get; set; }
}

class Mailing
{
    public int MailingId { get; set; }
    public string Name { get; set; }
}

class Physical
{
    public int PhysicalId { get; set; }
    public string Name { get; set; }
}

public void TestSOQuestion()
{
    string sql = @"select 1 as Id, 'hi' as Name, 1 as MailingId, 
       'bob' as Name, 2 as PhysicalId, 'bill' as Name";
    var item = connection.Query<Company, Mailing, Physical, Company>(sql,
                (org, mail, phy) =>
                {
                    org.Mailing = mail;
                    org.Physical = phy;
                    return org;
                },
                    splitOn: "MailingId,PhysicalId").First();


    item.Mailing.Name.IsEqualTo("bob");
    item.Physical.Name.IsEqualTo("bill");

}
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7078478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档