如何使用left join,aggregate和where子句创建Linq to Sql

gog*_*om3 1 c# database entity-framework linq-to-sql

有谁知道如何将此查询转换为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
Run Code Online (Sandbox Code Playgroud)

它在SQL中很简单,但是我无法将我的头围绕linq包装到sql.任何帮助,将不胜感激!

谢谢

Eni*_*ity 5

你想要这样的东西:

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;
Run Code Online (Sandbox Code Playgroud)