有没有一种方法可以在Dapper中一起使用MultiMapping和QueryMultiple? [英] Is there a way of using MultiMapping and QueryMultiple together in Dapper?

查看:127
本文介绍了有没有一种方法可以在Dapper中一起使用MultiMapping和QueryMultiple?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一起运行一些查询,可以使用 QueryMultiple 功能来实现。

I have a few queries that I need to run together and I can do so using the QueryMultiple feature.

但是在这种情况下我无法找出如何使用 MultiMapping

But in this case I've not been able to find out how could I use MultiMapping.

有人知道实现此目标的方法吗?

Does anyone know a way of achieving this?

推荐答案

我认为这就是您要查找的内容,尽管没有示例,您很难知道

I think this is what you're looking for though it's hard to tell without an example of the query you are trying to execute.

var sql = @"Select * 
            From Parent 
            Left Join Child on Child.ParentID = Parent.ParentID 
            Where Parent.ParentID = @id
            ... more queries";

using(var reader = connection.QueryMultiple(sql, new {id=selectedId}))
{
    var stuff = reader.Read<Parent, Child, Parent>(
        (p,c)=> 
        {
            p.Child = c;
            return p;
        }, splitOn: "ChildId").Single();
    // Continue to read from the other queries in your sql.
}

基本上 SqlMapper.GridReader 的方法类似于 Query 扩展方法。您只能使用带有两个以上泛型类型的重载之一获得 splitOn 参数。

Basically the Read method of the SqlMapper.GridReader is similar to the Query extension method. You only get the splitOn parameter with one of the overloads that takes more than two generic types.

这篇关于有没有一种方法可以在Dapper中一起使用MultiMapping和QueryMultiple?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆