2009-06-18 2 views
1

Ich habe drei Tabellen, Professoren, ProfessorStudent, Student.Entity Framework Viele-zu-Viele + Graf

Ich möchte alle Professoren + Wie viele Studenten jeder Professor haben.

Ich kann dies tun.

context.ProfessorSet.Include("Student") 

context.ProfessorSet.Include ("Student") ToList() werden alle drei Tabellen lesen.

Aber ich möchte nicht Student Tisch bekommen, ich will, dass Linq nur "Professor Table" + "Graf (*) ProfessorStudent Group By StudentId".

Es ist möglich?

 var c = from tag in contexto.ProfessorSet 
       select new 
       { 
        Tag = tag, 
        Count = tag.Student.Count 
       }; 

Aber erzeugen diese SQL:

SELECT C.Id, C.Nome, C.C1 VON (SELECT A

Antwort

2

ich dies tun mit .Id, A.Nome, (SELECT COUNT (0) VON ProfessorStudant A SB WHERE A.Id = B.ProfessorId ) AS [C1] VON Professor AS A)

Ich möchte dies:

Select A.Id, Graf (0) von Professor A inneren ProfessorStudent beitreten B auf A.Id = B.ProfessorId Gruppe von A.Id

+0

Danke @Fujiy es funktioniert perfekt für mich! –

0
from p in context.ProfessorSet 
from s in p.Student 
group s by s.StudentId into g 
select new 
{ 
    Professor = p, 
    StudentCounts = from sc in g 
        select new 
        { 
         StudentId = sc.Key, 
         Count = sc.Group.Count() 
        } 
} 
+0

Fehler auf den Leitungen Professor = p, Kann nicht Symbol p lösen und Count = sc.Count() Count() existiert nicht –

+0

Feste Anzahl. p ist richtig. –