Asp.net mvc 如何获得唯一的月份和年份组合?
使用VBNET、MVC 3和Entity Framework编写我的第一个MVC应用程序—一个单用户博客应用程序。我正在尝试创建一个存档边栏,它将呈现类似2011年10月的内容,当用户单击时,他们将在10月看到所有帖子。现在,我所有的尝试都显示了重复的日期——如果10月份有3篇帖子,那么我会看到2011年10月3次,或者我只得到了一个月-一年的组合,比如说2011年10月 使用groupby和firstordefault我只得到一个月的yaear组合Asp.net mvc 如何获得唯一的月份和年份组合?,asp.net-mvc,vb.net,asp.net-mvc-3,entity-framework,entity-framework-4,Asp.net Mvc,Vb.net,Asp.net Mvc 3,Entity Framework,Entity Framework 4,使用VBNET、MVC 3和Entity Framework编写我的第一个MVC应用程序—一个单用户博客应用程序。我正在尝试创建一个存档边栏,它将呈现类似2011年10月的内容,当用户单击时,他们将在10月看到所有帖子。现在,我所有的尝试都显示了重复的日期——如果10月份有3篇帖子,那么我会看到2011年10月3次,或者我只得到了一个月-一年的组合,比如说2011年10月 使用groupby和firstordefault我只得到一个月的yaear组合 posts = _rdsqlconn.Pos
posts = _rdsqlconn.Posts.Where(Function(p) p.PostIsPublished = True).GroupBy(Function(d) d.PostDatePublished).FirstOrDefault
如何使用EF获得独特的月-年组合
附加信息
我的存储库中有这个功能。我想拉月和年的配对,这样我就只有一对,比如说october,即使10月份有3个帖子。
在存储库中:
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
我会在C中尝试这个:
var yearMonths=\u rdsqlconn.Posts
.Wherep=>p.PostIsPublished
.GroupByp=>new{p.PostDatePublished.Year,p.PostDatePublished.Month}
.Selecta=>a.键
托利斯特先生;
它为您提供匿名对象的列表。每个对象都有一个Year和Month属性,例如yearMonths[0]。Year和yearMonths[0]。Month等。通过应用Select,您实际上会丢弃每个组中的元素,并且只会获得组键Year和Month的列表
编辑
我认为,对于您来说,最好的方法是为侧栏部分视图引入ViewModel。ViewModel将描述年和月组,例如:
公共类ArchiveMonthViewModel
{
公共整数年{get;set;}
公共整数月{get;set;}
}
然后,您不使用匿名类型分组,而是使用以下ViewModel类型:
var archiveViewModels=\u rdsqlconn.Posts
.Wherep=>p.PostIsPublished
.GroupByp=>新的ArchiveMonthViewModel
{
年份=p.PostDatePublished.Year,
月=p.PostDatePublished.Month
}
.Selecta=>a.键
托利斯特先生;
archiveViewModels现在是一个命名类型:列表,您可以从方法返回该列表:
公共IEnumerable SelectPostsByDate
{
//上面的代码。。。
返回archiveViewModels;
}
现在,您的局部视图应该基于IEnumerable和not IEnumerable类型的模型。在foreach循环@中,为模型中的每个项目拉出ArchiveMonthViewModel元素,这些元素现在是循环中的项目,然后使用item.Year和item.Month创建操作链接
希望您能将此草图翻译成VB。在VB中:
Dim groupedPosts = _rdsqlcon.Posts.
Where(Function(p) p.PostIsPublished = True).
GroupBy(Function(p) New With {p.PostDatePublished.Year, p.PostDatePublished.Month }).
Select(g => g.Key)
这只是返回唯一的年份和月份。如果要包含每个的帖子,请尝试以下操作:
Dim groupedPosts = _rdsqlcon.Posts.
Where(Function(p) p.PostIsPublished = True).
GroupBy(Function(p) New With {p.PostDatePublished.Year, p.PostDatePublished.Month
从那里,您可以显示年/月分组以及每个组的相关职位:
For Each group In groupedPosts
Console.WriteLine(group.Key.Year & "-" & group.Key.Month)
For Each post In group
Console.WriteLine(post.PostDatePublished)
Next
Next
可以发布等效的vbnet表达式吗。我无法将其转换为vbnet@Ashok:我也是:我想Jim Wooley已经在他的答案中发布了VB翻译。好的,这很有效-我只有一个月和一年的十月。由于我现在将月份和年份处理为int,是否有一种简单的方法将月份号转换为月份名称?@Ashok:请参见此处:现在它将-无法将类型为“System.Data.Entity.Infrastructure.DbQuery1[VB$AnonymousType_02[System.Int32,System.Int32]]”的对象转换为类型“System.Collections.Generic.IEnumerable`1”[RiderDesignMvcBlog.Core.Entities.Post]'。我仍收到此匿名类型错误消息。无法将类型为“System.Data.Entity.Infrastructure.DbQuery1[VB$AnonymousType_02[System.Int32,System.Int32]]”的对象强制转换为类型“System.Collections.Generic.IEnumerable1[RiderDesignMvcBlog.Core.Entities.Post]“.`@Ashok:上面的代码不可能出现这个错误。你到底在做什么?你是在使用一个现有的IEnumerable变量posts,而不是在查询中创建一个新实例吗?Dim posts很重要!你不能预先声明一个类型化变量,因为生成的类型是匿名的。@Slauma,这是entire函数:公共函数SelectPostsByDate As IEnumerableOf Post实现IPostRepository.SelectPostsByDate使用_rdsqlconnas New rdsqlconn Dim posts=_rdsqlconn.posts.WhereFunctionp p p.postSpublished=True.GroupByFunctionp New With{p.postDate created.Year,p.postDate created.Month}.选择功能g.键返回柱结束使用结束Function@Ashok:问题是您试图从方法返回匿名类型集合-这是不可能的,并导致您的错误。您的方法到底应该返回什么?我以为您想要一个年/月或组的组键集合。什么是你想要回复的帖子集吗?你可以编辑你的问题并在那里解释,这里的评论很难阅读。