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) };