如何按月显示帖子 asp.net mvc
how to display posts by month asp.net mvc
基本上我的posttable:
public partial class Mak
{
public string Name{ get; set; }
public DateTime writingDate{ get; set; }
}
我想在我的视图中列出 posts 分组的月份。
我的代码:
@foreach (var tar in Model.Mak.OrderByDescending(x=> x.writingDate.Month).Select(x=> x.writingDate))
{
<a href="/m/Date?year=@tar.Year&month=@tar.Month">@tar.ToString("MMMM yyyy")</a>
@foreach (var mak in Model.Mak.Where(x=> x.writingDate==tar))
{
<a href="/m/@mak.Name"></a><div> @mak.Name</div>
}
}
输出:
July 2017
Connected //post name
July 2017
Linq to SQL
July 2017
OOP
July 2017
Linq Querys
期望输出:
June 2017
xyz //post name
abc
July 2017
Connected
Linq to SQL
OOP
Linq Querys
我该如何写这个查询?
您可以尝试这样的操作:
@{
// gets the records grouped based on Year and Month.
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = Model.Mak
.OrderByDescending(x => x.writingDate)
.GroupBy(x => new { x.writingDate.Year, x.writingDate.Month });
// loop thorugh each group
foreach (var group in groupedByMonth)
{
// each group will have 2 "Keys" with Month and Year value.
// or use Url.Action() to form your anchor
<a href="/m/Date?year=@group.Key.Year&month=@group.Key.Month">@group.FirstOrDefault().writingDate.ToString("MMMM yyyy")</a> <br>
// looping through contents of group
// IGrouping inherits from IEnumerable, so this is possible
foreach (var mak in group)
{
<a href="/m/@mak.Name"><div> @mak.Name</div> </a><br>
}
}
}
使用月份和年份按 collection 分组,然后循环浏览分组的项目。
@{
var posts = Model.Mak; // Assuming this is the list of posts;
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
var postGrouped = posts.GroupBy(x => new { Month = x.WritingDate.Month,
Year = x.WritingDate.Year });
}
@foreach (var item in postGrouped)
{
<a href="@Url.Action("date","m",new { year=item.Key.Year, month=item.Key.Month})">
@dtfi.GetAbbreviatedMonthName(item.Key.Month) @item.Key.Year</a>
foreach (var post in item)
{
<div>
<a href="/m/@(post.Title)">@post.Title</a>
</div>
}
}
GetAbbreviatedMonthName
方法采用表示月份的整数和 return 字符串名称。
我还建议使用 html 辅助方法来呈现 link 的 href 值(使用 Url.Action
或 Html.ActionLink
辅助方法)。在编写 C# 类 时,还要考虑遵循 PascalCasing 方法。 (WritingDate
而不是 writingDate
)
基本上我的posttable:
public partial class Mak
{
public string Name{ get; set; }
public DateTime writingDate{ get; set; }
}
我想在我的视图中列出 posts 分组的月份。
我的代码:
@foreach (var tar in Model.Mak.OrderByDescending(x=> x.writingDate.Month).Select(x=> x.writingDate))
{
<a href="/m/Date?year=@tar.Year&month=@tar.Month">@tar.ToString("MMMM yyyy")</a>
@foreach (var mak in Model.Mak.Where(x=> x.writingDate==tar))
{
<a href="/m/@mak.Name"></a><div> @mak.Name</div>
}
}
输出:
July 2017
Connected //post name
July 2017
Linq to SQL
July 2017
OOP
July 2017
Linq Querys
期望输出:
June 2017
xyz //post name
abc
July 2017
Connected
Linq to SQL
OOP
Linq Querys
我该如何写这个查询?
您可以尝试这样的操作:
@{
// gets the records grouped based on Year and Month.
// Creates an object of IEnumerable<IGrouping<>> type
var groupedByMonth = Model.Mak
.OrderByDescending(x => x.writingDate)
.GroupBy(x => new { x.writingDate.Year, x.writingDate.Month });
// loop thorugh each group
foreach (var group in groupedByMonth)
{
// each group will have 2 "Keys" with Month and Year value.
// or use Url.Action() to form your anchor
<a href="/m/Date?year=@group.Key.Year&month=@group.Key.Month">@group.FirstOrDefault().writingDate.ToString("MMMM yyyy")</a> <br>
// looping through contents of group
// IGrouping inherits from IEnumerable, so this is possible
foreach (var mak in group)
{
<a href="/m/@mak.Name"><div> @mak.Name</div> </a><br>
}
}
}
使用月份和年份按 collection 分组,然后循环浏览分组的项目。
@{
var posts = Model.Mak; // Assuming this is the list of posts;
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
var postGrouped = posts.GroupBy(x => new { Month = x.WritingDate.Month,
Year = x.WritingDate.Year });
}
@foreach (var item in postGrouped)
{
<a href="@Url.Action("date","m",new { year=item.Key.Year, month=item.Key.Month})">
@dtfi.GetAbbreviatedMonthName(item.Key.Month) @item.Key.Year</a>
foreach (var post in item)
{
<div>
<a href="/m/@(post.Title)">@post.Title</a>
</div>
}
}
GetAbbreviatedMonthName
方法采用表示月份的整数和 return 字符串名称。
我还建议使用 html 辅助方法来呈现 link 的 href 值(使用 Url.Action
或 Html.ActionLink
辅助方法)。在编写 C# 类 时,还要考虑遵循 PascalCasing 方法。 (WritingDate
而不是 writingDate
)