如何使用左连接、聚合和 where 子句创建 Linq to Sql
本文关键字:子句 where 创建 Linq Sql to 何使用 连接 | 更新日期: 2023-09-27 18:36:45
有谁知道如何将此查询转换为 LINQ to SQL?
SELECT posts.*, count(COMMENTS.*) AS comment_count FROM POSTS
LEFT JOIN COMMENTS on POSTS.id = COMMENTS.post_id
WHERE comments.date IS NULL OR comments.date >= [NOW]
GROUP BY posts.id
ORDER BY comment_count DESC
它在SQL中很简单,但是我很难理解linq to sql。任何帮助将不胜感激!
谢谢
你想要这样的东西:
var query =
from p in POSTS
join c in COMMENTS on p.id equals c.post_id into cs
group new
{
Post = p,
Comments = cs
.Where(c1 => c1.date >= DateTime.Now)
.Count(),
} by p.id;
只是一个建议,你可以随时使用这个伟大的工具
http://www.sqltolinq.com/
帮助转换