具有group by和max到C#linq的嵌套查询

时间:2016-10-13 15:11:31

标签: c# linq sql-to-linq-conversion

我有以下TSQL

SELECT COUNT(M.MuseumID) as [ACTIVECARDHOLDERS] 
FROM Museum AS M
WHERE M.MuseumID IN
     (SELECT MAX(X.MuseumID) AS MuseumID 
      FROM (SELECT BioStation.[MuseumID]
                  ,BioStation.[PersonID] 
                  ,ISNULL(M.ISCARDINVALIDATED,0) AS [ISCARDINVALIDATED] 
             FROM Museum AS M
                  INNER JOIN BioStation ON BioStation.MuseumID = Museum.MuseumID
             WHERE M.CASESTATE=7 AND M.CASESTATUS=6) AS X 
      GROUP BY X.PERSONID) 
AND ISNULL(M.ISCARDINVALIDATED,0)=0;

我试图在C#中转换为linq,从最内层的查询开始,但是我不确定如何将它们嵌套在带有group \ max的linq中。这就是我到目前为止所没有的错误:

var query = (from m in dbContext.Museum
             join b in dbContext.BioStation on m.MuseumID equals b.MuseumID
             where m.CaseState == 7 && m.CaseStatus == 6
             select new {b.MuseumID, b.PersonID, IsCardInvalidated = (m.IsCardInvalidated == null ? false : m.IsCardInvalidated) }).AsQueryable();

感谢您对翻译的任何帮助,谢谢。

0 个答案:

没有答案