使用VBNET,MVC 3和Entity Framework来编写我的第一个mvc应用程序 - 一个用户博客应用程序。我正在尝试创建一个归档边栏,它将呈现类似2011年10月的内容,当用户点击时,他们将在十月份看到所有帖子。现在我所有的尝试都显示重复的日期 - 如果有10个职位在十月然后我看到十月2011年3次或我只回来一个月年组合说2011年十月。如何获得唯一的月份和年份组合?
使用groupby with firstordefault我只得到一个月yaear组合。
posts = _rdsqlconn.Posts.Where(Function(p) p.PostIsPublished = True).GroupBy(Function(d) d.PostDatePublished).FirstOrDefault
我该如何找回与EF独特的月度连续剧?
其他信息
我有我的存储库的功能。我想拉动月份和年份对,这样即使在10月有3个帖子,我也只有一对说ocotober。 在仓库:
Public Function SelectPostsByDate() As IEnumerable(Of Entities.Post) Implements Interfaces.IPostRepository.SelectPostsByDate
Using _rdsqlconn As New RDSQLConn
Dim posts
posts = _rdsqlconn.Posts.Where(Function(p) p.PostIsPublished = True).GroupBy(Function(p) New With {p.PostDateCreated.Year, p.PostDateCreated.Month}).Select(Function(g) g.Key)
'posts = _rdsqlconn.Posts.Where(Function(p) p.PostIsPublished = True).GroupBy(Function(p) New With {p.PostDatePublished.Value.Year, p.PostDatePublished.Value.Month})
Return posts
End Using
End Function
在我的控制,我有
Function DateViewPartial() As PartialViewResult
Return PartialView(_postRepository.SelectPostsByDate)
End Function
我的部分观点有:
@ModelType IEnumerable (of RiderDesignMvcBlog.Core.Entities.Post)
<hr />
<ul style="list-style: none; margin-left:-35px;">
@For Each item In Model
@<li> @Html.ActionLink(item.PostDatePublished.Value.ToString("Y"), "Archives", "Blog", New With {.year = item.PostDatePublished.Value.Year, .month = item.PostDatePublished.Value.Month}, Nothing)</li>
Next
</ul>
在_Layout.vbhtml我所说的局部视图在侧边栏呈现:
<h3>Posts by Date</h3>
@code
Html.RenderAction("DateViewPartial", "Blog")
End Code
我已经编辑我的回答。 – Slauma