2012-04-13 10 views
2

Ich versuche, die folgende SQL-Abfrage in Linq zu reproduzieren und brauche bitte etwas Hilfe.Mehrere äußere Verknüpfung mit Linq mit 2 Joins zur gleichen Tabelle/Objekt. Habe die SQL, brauche die Linq zu Entity

select 
    dbo.Documents.DocId, 
    dbo.Documents.ReykerAccountRef, 
    dbo.Documents.ReykerClientId DocClientID, 
    CAAs.ClientId CAAClientIDCheck, 
    ClientData.FullName ClientFullName, 
    CAAs.IFAId, 
    AdvisorData.FullName AdvisorFullName 
from dbo.Documents 
left join 
    dbo.CAAs on dbo.Documents.ReykerAccountRef = dbo.CAAs.AccountRef 
left join 
    dbo.hmsProfileDatas AS ClientData 
on 
    dbo.CAAs.ClientId = ClientData.ReykerClientID 
left join 
    dbo.hmsProfileDatas AS AdvisorData 
on 
    dbo.CAAs.IFAId = AdvisorData.ReykerClientID 

Ich versuche, auf den gleichen Tisch für einen Kunden Fullname und das andere nach einem Berater Fullname zweimal, einmal zu verknüpfen.

Die grundlegende SQL ich in Linq produzieren wollen, ist

select table1.*,table2.*,a.Fullname, b.Fullname 
from table1 
left join 
    table2 on table1.t2Id = table2.Id 
left join 
    table3 AS a 
on 
    table2.t3Id1 = table3.id1 
left join 
    table3 AS b 
on 
    table2.t3Id2 = table3.id2 

So Tisch ein bis table2 verbunden ist und table2 hat zwei Fremdschlüssel (t3Id1 und t3Id2) zu verschiedenen Bereichen (ID1 und ID2) Table3.

Dies ist, was ich versucht habe, einige Leitlinien zu folgen, aber es gibt nichts zurück! Was läuft falsch?

 var results3 = from doc in DataContext.Documents 
         from caa 
          in DataContext.CAAs 
          .Where(c => c.AccountRef == doc.ReykerAccountRef) 
          .DefaultIfEmpty() 
         from cpd 
          in DataContext.hmsProfileDatas 
          .Where(pdc => pdc.ReykerClientID == caa.ClientId) 
          .DefaultIfEmpty() 
         from apd 
          in DataContext.hmsProfileDatas 
          .Where(pda => pda.ReykerClientID == caa.IFAId) 
          .DefaultIfEmpty() 
         select new DocumentInList() 
            { 
             DocId = doc.DocId, 
             DocTitle = doc.DocTitle, 
             ReykerDocumentRef = doc.ReykerDocumentRef, 
             ReykerAccountRef = doc.ReykerAccountRef, 
             ClientFullName = cpd.FullName, 
             AdvisorFullName = apd.FullName, 
             DocTypeId = doc.DocTypeId, 
             DocTypes = doc.DocTypes, 
             DocDate = doc.DocDate, 
             BlobDocName = doc.BlobDocName, 
             UploadDate = doc.UploadDate, 
            }; 
+0

Hast du ‚get‘ die SQL von Linq-Abfrage und versuchen Sie es in der Db – NSGaga

+0

Nr ich dies gegen SQL Azure bin mit und haben nicht funktioniert, wie die SQL zu sehen. Es gibt kein Profiling. Überprüfen Sie das jetzt auf dem Management-Panel! – NER1808

+0

aber Sie könnten es immer noch lokal auf jedem Db, CE zum Beispiel testen - empfohlen, um Ihre Sachen gerade zu bekommen. Mit EF/CF ist es einfach, Dinge zu "bewegen", also warum nicht. – NSGaga

Antwort

11

Ich hoffe, ich habe Ihr Beispiel richtig verstanden. Hier ist ein weiteres einfaches Beispiel soll das geben, was Sie brauchen:

private class User 
    { 
     public int UserId; 
     public string Name; 
     public int GroupId; 
     public int CollectionId; 
    } 

    public class Group 
    { 
     public int GroupId; 
     public string Name; 
    } 

    public class Collection 
    { 
     public int CollectionId; 
     public string Name; 
    } 

    static void Main() 
    { 
     var groups = new[] { 
      new Group { GroupId = 1, Name = "Members" }, 
      new Group { GroupId = 2, Name = "Administrators" } 
     }; 
     var collections = new[] { 
      new Collection { CollectionId = 1, Name = "Teenagers" }, 
      new Collection { CollectionId = 2, Name = "Seniors" } 
     }; 
     var users = new[] { 
      new User { UserId = 1, Name = "Ivan", GroupId = 1, CollectionId = 1 }, 
      new User { UserId = 2, Name = "Peter", GroupId = 1, CollectionId = 2 }, 
      new User { UserId = 3, Name = "Stan", GroupId = 2, CollectionId = 1 }, 
      new User { UserId = 4, Name = "Dan", GroupId = 2, CollectionId = 2 }, 
      new User { UserId = 5, Name = "Vlad", GroupId = 5, CollectionId = 2 }, 
      new User { UserId = 6, Name = "Greg", GroupId = 2, CollectionId = 4 }, 
      new User { UserId = 6, Name = "Arni", GroupId = 3, CollectionId = 3 }, 
     }; 

     var results = from u in users 
         join g in groups on u.GroupId equals g.GroupId into ug 
         from g in ug.DefaultIfEmpty() 
         join c in collections on u.CollectionId equals c.CollectionId into uc 
         from c in uc.DefaultIfEmpty() 
         select new { 
          UserName = u.Name, 
          GroupName = g != null ? g.Name : "<No group>", 
          CollectionName = c != null ? c.Name : "<No collection>" 
         }; 
    } 

Es produziert zwei Join über eine Tabellendaten aus anderen beiden Tabellen zu erhalten. Hier ist der Ausgang:

Ivan Members   Teenagers 
Peter Members   Seniors 
Stan Administrators Teenagers 
Dan  Administrators Seniors 
Vlad <No group>  Seniors 
Greg Administrators <No collection> 
Arni <No group>  <No collection> 
+0

Das sieht gut aus. Vielen Dank. Ich habe den Einsatz vorher nicht bemerkt. – NER1808

+0

Endlich jemand, der ein einfaches Beispiel für mich geschrieben hat :) – aup