2017-05-26 59 views
0

我已經創建了一個SQL查詢,如下所示:轉換SQL內部聯接,GROUP BY和秩序LINQ

SELECT s.FullName, s.[Id], COUNT(t.[User]) AS Records 
FROM dbo.AspNetUsers s INNER JOIN dbo.PBBuilds t 
ON s.Id = t.[User] 
GROUP BY t.[User], s.[FullName], s.[Id] 
ORDER BY COUNT(t.[User]) DESC 

我試圖將其轉換爲LINQ查詢,這是多遠我有:

Dim results = From H In context.Users 
      Join C In context.PBBuilds On H.Id Equals C.User 
      Group By C.User Into 

任何人都可以請指教。

回答

1

嘗試這個查詢 - (修改相應雖然)

var results = From U In context.AspNetUsers 
     Join P In context.PBBuilds On U.Id Equals P.User 
     Group By new { P.User, U.FullName } Into g 
     Orderby COUNT(P.User) Descending 
     select new { 
     FullName = U.FullName, 
     ID = U.Id, 
     Records = COUNT(P.User) 
     }; 
+0

錯誤LINQ查詢 –

+0

如何用P和U –

+0

選擇值ca't組用於克對象 –

0

try代碼

var results = From U In context.AspNetUsers 
    Join P In context.PBBuilds On U.Id Equals P.User 
    Group new {U,P} By new { P.User, U.FullName } Into g  
    select new { 
    FullName = g.FirstOrDefault().U.FullName, 
    ID = g.FirstOrDefault().U.Id, 
    Records = g.Count() 
    }).OrderByDescending(c=>c.Records).ToList();