Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of using MultiMapping and QueryMultiple together in Dapper?

Tags:

dapper

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

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?

like image 692
Carlos Mendes Avatar asked Apr 25 '12 15:04

Carlos Mendes


1 Answers

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.
}

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.

like image 88
juharr Avatar answered Oct 01 '22 13:10

juharr