我如何使用Entity Framework 5编写

时间:2014-07-05 16:15:38

标签: entity-framework-5

我有一个像这样的SQL代码,

SELECT *,
(Select Count(PostID) FROM Likes Where Likes.PostID = Posts.PostID) AS Likes,
(Select Count(PostID) FROM Comments WHERE Comments.PostID = Posts.PostID) AS Comments
FROM Posts

但是我需要用EntityFramework 5编写这段代码。我怎么能写这段代码? 感谢。

1 个答案:

答案 0 :(得分:0)

from post in Posts
select new
{
 Post = post,
 Likes = (from like in Likes where like.PostID == post.PostID select like).Count(),
 Comments = (from comment in Comments where comment.PostID = post.PostID select post).Count()
}