0
我正在使用LINQ從數據庫中獲取記錄,我已經在SQL服務器上編寫了一個SQL查詢,我正在使用asp.net .NET MVC編寫LINQ查詢,我正在使用由左組的加入,並在查詢次數所以說,它不具有範圍變量的選擇,我已經嘗試了很多東西,但失敗了,以下是查詢通過左連接使用組和LINQ計數
SQL查詢:
select Post_ID, Post, com.cmtcount from Posts left join (select Comments.postID,
Count(Comments.comments) as cmtcount from Comments group by postID)
as com on com.postID = Post_ID;
Linq查詢:
from row in db.Posts join cmtrow in db.Comments on row.Post_ID
equals cmtrow.postID into ps from cmtrow in ps.DefaultIfEmpty()
group cmtrow by cmtrow.Comments_ID into grouped select new
{ row.Post1, row.Posted_by,cmtrow.comments,
count = grouped.Count(t=>t.Comments_ID!=null) };