1

Fondamentalement ma table post:comment afficher les messages de mois asp.net mvc

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

Je veux la liste des postes regroupés mois avec pour chacun sur mon point de vue.
Mon code:

@foreach (var tar in Model.Mak.OrderByDescending(x=> x.writingDate.Month).Select(x=> x.writingDate)) 
{ 
    <a href="/m/[email protected]&[email protected]">@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> 
    } 

} 

Sortie:

July 2017 
Connected //post name 

    July 2017 
Linq to SQL 

    July 2017 
OOP 

    July 2017 
Linq Querys 

sortie souhaitée:

June 2017 
xyz //post name 
abc  
    July 2017 
Connected 
Linq to SQL 
OOP 
Linq Querys 

Comment devrais-je écrire cette requête?

Répondre

0

Groupe par votre collection en utilisant mois et l'année, puis une boucle sur les éléments groupés.

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

La méthode GetAbbreviatedMonthName prend l'entier représentant le mois et retourner le nom de chaîne.

Je recommande également d'utiliser les méthodes d'aide html pour afficher la valeur href (utiliser les méthodes auxiliaires Url.Action ou Html.ActionLink) du lien. Pensez également à suivre l'approche PascalCasing lors de l'écriture de classes C#. (WritingDate au lieu de writingDate)

+0

merci pour les conseils bro, laissez-moi poser une question à vous, pourquoi devrais-je suivre l'approche PascalCasing? – Anil

+0

Vous n'avez pas besoin de. Mais c'est ce que l'industrie fait. Voir ce que Microsoft fait https://github.com/dotnet/docs/blob/master/docs/csharp/programming-guide/inside-a-program/coding-conventions.md – Shyju

+0

merci, je regarde ces conventions – Anil

0

Vous pouvez essayer quelque chose comme ceci:

@{ 
    // 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/[email protected]&[email protected]">@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> 
     } 
    } 
} 
+0

merci, cela fonctionne – Anil