【问题标题】:how to display posts by month asp.net mvc如何按月显示帖子asp.net mvc
【发布时间】:2017-09-13 15:47:25
【问题描述】:

基本上是我的post表:

public partial class Mak
{
    public string Name{ get; set; }        
    public DateTime writingDate{ get; set; }
}

我想在我的视图中列出按月份分组的帖子。
我的代码:

@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

我应该如何编写这个查询?

【问题讨论】:

    标签: asp.net-mvc entity-framework linq linq-to-sql linq-to-entities


    【解决方案1】:

    你可以试试这样的:

    @{
        // 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>
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用月份和年份按您的集合分组,然后遍历分组的项目。

      @{
          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 方法接受表示月份的整数并返回字符串名称。

      我还建议使用 html 辅助方法来呈现链接的 href 值(使用 Url.ActionHtml.ActionLink 辅助方法)。在编写 C# 类时,还要考虑遵循 PascalCasing 方法。 (WritingDate 而不是writingDate

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多