2011-10-18 75 views
1

如何在VB.NET LINQ中編寫此SQL查詢?如何在VB.NET中編寫此SQL查詢LINQ

SELECT COUNT(*) AS COUNT_NO 
FROM A INNER JOIN B ON A.ID = B.ID 
WHERE A.SERIAL_NO IN (SELECT MAX (A.SERIAL_NO) 
         FROM A INNER JOIN B ON A.ID = B.ID 
         GROUP BY A.SUBJECT_ID) 
AND B.STATUS = 'N'; 

感謝

+0

查詢中「group by」的用法是什麼? –

回答

0

好了,直接合理的C#轉換會是這樣的:

var validSerialNumbers = from a in dbContext.A 
         join b in dbContext.B on a.ID equals b.ID 
         group a.SerialNo by a.SubjectId into g 
         select g.Max(); 

var query = from a in dbContext.A 
      join b in dbContext.B on a.ID equals b.ID 
      where validSerialNumbers.Contains(a.SerialNo) 
      where b.Status == "N" 
      select new { a, b }; 

var count = query.Count(); 

我會想象你可以進行轉換的VB非常相似。不幸的是,Reflector在這個方面做得不好,但是就我所知,這些條款應該換算爲1。

+0

感謝它正在工作現在.... –