1

使用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 
+0

我已经编辑我的回答。 – Slauma

回答

0

我会尝试这个(在C#):

var yearMonths = _rdsqlconn.Posts 
    .Where(p => p.PostIsPublished) 
    .GroupBy(p => new { p.PostDatePublished.Year, p.PostDatePublished.Month }) 
    .Select(a => a.Key) 
    .ToList(); 

它可以让你匿名对象的列表。每个对象具有YearMonth属性 - 例如yearMonths[0].YearyearMonths[0].Month等。通过应用Select,您实际上丢弃了每个组中的元素,并且只获得组键(年和月)的列表。

编辑

我想,你的目的最好的办法是引入“视图模型”为您的侧边栏局部视图。该ViewModel形容的年份和月份团,例如:

public class ArchiveMonthViewModel 
{ 
    public int Year { get; set; } 
    public int Month { get; set; } 
} 

那么你不组一个匿名类型,但使用这个视图模型类型:

var archiveViewModels = _rdsqlconn.Posts 
    .Where(p => p.PostIsPublished) 
    .GroupBy(p => new ArchiveMonthViewModel 
    { 
     Year = p.PostDatePublished.Year, 
     Month = p.PostDatePublished.Month 
    }) 
    .Select(a => a.Key) 
    .ToList(); 

archiveViewModels现在是一个命名类型: List<ArchiveMonthViewModel>你可以从你的方法返回:

public IEnumerable<ArchiveMonthViewModel> SelectPostsByDate() 
{ 
    // code above ... 
    return archiveViewModels; 
} 

现在你的部分观点,应根据IEnumerable<ArchiveMonthViewModel>类型的模型(而不是IEnumerable<Post>)。在您的foreach循环中(@For Each item In Model),您现在抽出循环中的ArchiveMonthViewModel元素即item,然后使用item.Yearitem.Month创建操作链接。

(希望你能翻译此草图到VB。)

+0

你可以发布等效的vbnet表达式吗?我无法将其转换为vbnet –

+0

@Ashok:我也是:)我认为Jim Wooley已经在他的答案中发布了VB翻译。 – Slauma

+0

行得通 - 我十月只有一个月和一年。由于我现在处理的月份和年份是int,有没有简单的方法将月份数字转换为月份名称? –

0

在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 
+0

现在它抛出 - 无法强制类型'System.Data.Entity.Infrastructure.DbQuery'1 [VB $匿名类型_0'2 [System.Int32,System.Int32]]'类型的对象类型'System.Collections.Generic.IEnumerable '1 [RiderDesignMvcBlog.Core.Entities.Post]”。 –

+0

我仍然收到这个匿名类型的错误消息。 '无法投射'System.Data.Entity.Infrastructure.DbQuery'1 [VB $ AnonymousType_0'2 [System.Int32,System.Int32]]'类型的对象来键入'System.Collections.Generic.IEnumerable'1 [RiderDesignMvcBlog .Core.Entities.Post]'。' –

+0

@Ashok:用上面的代码得到这个错误是不可能的。你到底在做什么?您是否正在使用现有变量'posts',它是'IEnumerable '而不是使用查询创建新实例? '小帖子'很重要!您不能预先声明类型变量,因为结果类型是匿名的。 – Slauma