如何在VB.NET LINQ中编写这个SQL查询

时间:2011-10-18 05:40:57

标签: vb.net linq

如何在VB.NET LINQ中编写此SQL查询?

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';

由于

1 个答案:

答案 0 :(得分: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对1。