How to retrive all comments for a specific post?
Having a bit of block trying to get comments for a specific post. Working with MVC 3 and VBNET. A post url looks like /Blog/Post/1. I can display the post without problem but need to get the comments for PostId=1 from the Comments table. I tried a Linq inner join statement
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()
开发者_Go百科 End Sub
Property Posts As DbSet(Of Post)
Property Categories As DbSet(Of Category)
Property Comments As DbSet(Of Comment)
End Class
But this throws:
`Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[RiderDesignMvcBlog.Core.Entities.Post]' to type 'RiderDesignMvcBlog.Core.Entities.Post'.`
However a SQL query such as the one below works just fine. Can i just pass in this sql statement to my EF?
Select * From Posts INNER JOIN Comments on dbo.Posts.PostId = Comments.PostId where dbo.Posts.PostId = 1
Dim results = From P In _rdsqlconn.Posts Join c In _rdsqlconn.Comments On P.PostId Equals c.PostId
Where P.PostId = id
Select P
精彩评论