2012-04-25 4 views

risposta

10

Credo che questo è ciò che stai cercando se è difficile dire, senza un esempio della query che si sta tentando di eseguire.

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, splitOn: "ChildId"); 
    // Continue to read from the other queries in your sql. 
} 

Fondamentalmente il metodo della SqlMapper.GridReaderRead è simile al metodo Query estensione. Si ottiene solo il parametro splitOn con uno degli overload che richiede più di due tipi generici.

1

Theres un rapido esempio preso da un altro thread: how-to-get-values-for-child-objects

var sql = 
@" 
select * from PROFILES where profileId= @id 
select * from PROFILEIMAGES where OWNER_PROFILESIDFK = @id"; 

using (var multi = connection.QueryMultiple(sql, new {id=selectedId})) 
{ 
    var profile = multi.Read<Models.PROFILE>().Single(); 
    profile.ProfileImages = multi.Read<Model.PROFILEIMAGES>().ToList(); 
} 

Ogni query restituisce un insieme di oggetti che poi possono essere mappati alle vostre entità.

+0

Grazie Alex. Ma sto cercando un modo per utilizzare la funzione [Multimapping] (https://github.com/SamSaffron/dapper-dot-net/blob/master/Tests/Tests.cs#L459) –

+0

Potresti fornire un esempio su cosa stai cercando di fare? – Alex

+1

Fondamentalmente memorizzo alcuni campi denormalizzati in SQL (es .: un campo DTags in cui ho tutti i tag separati da ";", ecc.). E quando utilizzo QueryMultiple (metodo di lettura) non è possibile utilizzare il parametro "split" come nelle query MultiMapping –

Problemi correlati