有人知道如何将这个查询转换成linq到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;

关于c# - 如何使用左连接,聚合和where子句创建Linq to Sql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10966479/

10-15 06:16