1
我的SQL語句是如何實現這個SQL塊包含(有,加入和GROUP BY)使用LINQ
SELECT c.type,c.title,c.datereg, d.ranknum
FROM T_News AS c
INNER JOIN (
SELECT a.id, COUNT(*) AS ranknum
FROM T_News AS a
INNER JOIN T_News AS b
ON (a.type = b.type)
AND (a.datereg >= b.datereg)
GROUP BY a.id
HAVING COUNT(*) <= 3
) AS d ON (c.id = d.id)
ORDER BY c.type, d.ranknum
,我得到http://rickosborne.org/blog/2008/01/sql-getting-top-n-rows-for-a-grouped-query/
爲獲得TOP N行的分組查詢
EFUnitOfWork EF = new EFUnitOfWork();
T_NewsRepository News = new T_NewsRepository();
News.UnitOfWork = EF;
var query =
from news1 in News.All()
join news2 in News.All()
on news1.type equals news2.type into resjoin
group news1 by news1.id into idgroup
where idgroup.Count() <= 3
select new { idgroup };
var x = query.ToList();
我沒有得到任何錯誤,但「哪裏idgroup.Count()< = 3」,沒有工作,我得到的所有行以dB爲結果
你得到了什麼錯誤? – doogle 2012-02-03 12:14:22
我沒有得到任何錯誤,但「其中idgroup.Count()<= 3」沒有工作,我得到所有行數據庫作爲結果 – amir 2012-02-03 13:04:38