开发者

How convert sql query to linq-to-sql

I have Sql query:

SELECT News.NewsId,开发者_运维百科 News.Subject, Cm.Cnt FROM dbo.News
LEFT JOIN 
(SELECT Comments.OwnerId, COUNT(Comments.OwnerId) as Cnt 
 FROM Comments 
 WHERE Comments.CommentType = 'News' 
 Group By Comments.OwnerId) Cm
ON Cm.OwnerId = News.NewsId

But I want linq-to-sql query, how I can convert this to linq?


You might find it helpful to download LinqPad, which will make it extremely easy to write LINQ and test it. It will also help you learn the syntax. Awesome tool. And it's free.


how I can convert this to linq?

Practice! :)

from news in News
let count = news.Comments
  .Where(comment => comment.CommentType == "News")
  .Count()
select new {news.NewsId, news.Subject, Count = count}; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜