2017-10-16 6 views
0

Versuchen, das herauszufinden, aber ich kann es nicht zur Arbeit bekommen. Diese Abfrage:Mapping eins zu vielen mit Dapper

select MultiCollections.*, Collections.* from MultiCollections 
left join MultiCollectionCollections on MultiCollections.Id = MultiCollectionCollections.MultiCollectionId 
left join Collections on MultiCollectionCollections.CollectionId = Collections.Id 
where MultiCollections.UserId=5 

Dieser diese Daten zurückgibt:

enter image description here Wie Sie sehen können, Reihe 1 und 2 sind aus dem gleichen Titel. Die Daten dahinter sind Bücher. Zeile 3 und 4 sind auch Sammlungen, haben aber keine Bücher.

Ich habe zwei Objekte in meinem Code: MultiCollection Sammlung

Sowohl mit den Daten im Ergebnis der Abfrage angegeben entsprechen: Id, Benutzer-ID und Titel sind für das Objekt MultiCollection. Andere Daten sind für das Objekt Sammlung.

Ich erwarte, dass drei MultiCollections in meinem C# -Code zu sehen: Aktion Drama- Fiction

Aktion werden zwei Sammlungen haben. Drama und Fiktion sollte leer sein.

Stattdessen erhalte ich 4 MultiCollections und keiner von ihnen enthält Sammlungen. Mein C# -Code:

public IEnumerable<MultiCollection> GetAll(int userId) 
    { 
     string query = @"select MC.*, C.* from MultiCollections MC 
         left join MultiCollectionCollections MCC on MC.Id = MCC.MultiCollectionId 
         left join Collections C on MCC.CollectionId = C.Id 
         where UserId=" + userId; 

     using (DbConnection connection = ConnectionFactory()) 
     { 
      connection.Open(); 
      return connection.Query<MultiCollection, List<Collection>, MultiCollection>(query, 
       (a, s) => 
       { 
        a.Collections = s; 
        return a; 
       }); 
     } 
    } 

Wenn der Code ausgeführt wird ich dies erwarten würde:

Action  
    Collections  
     -> Book 1  
     -> Book 2  
Drama  
    Collections  
     Null  
Fiction  
    Collections  
     Null 

Ich habe keine Ahnung, was ich falsch mache.

+0

Welcher Typ ist 'a.Collections'? Ist es eine 'Liste '? – 12seconds

+0

Yup, Liste

Antwort

0

Ihre C# Code sollte wie folgt aussehen von:

public IEnumerable<MultiCollection> GetAll(int userId) 
{ 
    string query = @"select MC.*, C.* from MultiCollections MC 
        left join MultiCollectionCollections MCC on MC.Id = MCC.MultiCollectionId 
        left join Collections C on MCC.CollectionId = C.Id 
        where UserId=" + userId; 

    using (DbConnection connection = ConnectionFactory()) 
    { 
     connection.Open(); 
     return connection.Query<MultiCollection, Collection, MultiCollection>(query, 
      (a, s) => 
      { 
       a.Collections = new List<Collection>(); 
       a.Collections.Add(s); 
       return a; 
      }, splitOn: "MultiCollectionId,CollectionId");); 
    } 
} 

Hinweis, dass .Query<MultiCollection, Collection, MultiCollection> ist ein Collection nicht List<Collection> und es tut .add() und nicht Setter.

+0

Jetzt bekomme ich noch 4 MultiCollections statt 3. Die ersten 2 sind gleich mit jeweils dem entsprechenden Buch. Aber beide Bücher sollten unter einer Multikollektion hinzugefügt werden. –

+0

Ps. a.Collections.add (s) sollte a.Collections.Add (s) (capital) sein und a.Collections ist beim ersten Start Null, also sollte initialisiert werden ... Einfach gesagt, keine harten Gefühle –

+0

@KenjiElzerman, yah, np. Ich tippe es ohne irgendeine IDE. Nur auf meinem Kopf. – 12seconds