开发者

How convert sql query to linq-to-sql

开发者 https://www.devze.com 2023-01-01 19:47 出处:网络
I have Sql query: SELECT News.NewsId,开发者_运维百科 News.Subject, Cm.Cnt FROM dbo.News LEFT JOIN

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

精彩评论

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