如何检索特定帖子的所有评论?

时间:2011-09-30 12:08:55

标签: sql-server asp.net-mvc vb.net entity-framework

尝试获取特定帖子的评论。使用MVC 3和VBNET。帖子网址看起来像/ Blog / Post / 1。我可以毫无问题地显示帖子,但需要从评论表中获取PostId = 1的评论。我尝试了一个Linq内连接语句

Dim results = From P In _rdsqlconn.Posts Where P.PostId = id Join c In _rdsqlconn.Comments On P.PostId Equals c.PostId Select P



Public Class RDSQLConn
        Inherits DbContext

        Public Sub New()

        End Sub


        Property Posts As DbSet(Of Post)
        Property Categories As DbSet(Of Category)
        Property Comments As DbSet(Of Comment)

    End Class

但是这引发了:

`Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[RiderDesignMvcBlog.Core.Entities.Post]' to type 'RiderDesignMvcBlog.Core.Entities.Post'.`

然而,如下所示的SQL查询工作正常。我可以将这个sql语句传递给我的EF吗?

Select * From Posts  INNER JOIN Comments on dbo.Posts.PostId = Comments.PostId where dbo.Posts.PostId = 1 

1 个答案:

答案 0 :(得分:0)

Dim results = From P In _rdsqlconn.Posts Join c In _rdsqlconn.Comments On P.PostId Equals c.PostId 
Where P.PostId = id 
Select P