display data sorted by date

后端 未结 3 520
庸人自扰
庸人自扰 2021-01-24 10:05
public class Person
{
    public string Name {get; set;}                
    public DateTime Created { get; set; }
}

public class MyData
{
   public List          


        
相关标签:
3条回答
  • 2021-01-24 10:24

    You need to group the Person objects by entire days, so let's do:

    var res = Model.Persons.OrderByDescending(p => p.Created)
       .GroupBy(p => p.Created.ToShortDateString());
    

    To display them on the console:

    foreach(var entry in res)
    {
        // Day is the same for all items in a group entry.
        string groupDay = entry.First().Created.ToShortDateString();
        Console.WriteLine(groupDay);
    
        // list all names of persons in that group
        foreach(Person p in entry)
        {
            Console.WriteLine(p.Name);
        }
    }
    
    0 讨论(0)
  • 2021-01-24 10:30

    I belive you would want to use groupBy so you can have the results by date :

     var query = Persons.OrderByDescending(c => c.Created).GroupBy(n=> n.Created.ToShortDateString());
    
     foreach (var d in query)
            {
                Console.WriteLine(d.Key);
                foreach (var names in d)
                   Console.WriteLine(names.name);
            }
    
    0 讨论(0)
  • 2021-01-24 10:33

    you can do like:

    Persons.OrderBy(p => p.CreatedDate);
    
    0 讨论(0)
提交回复
热议问题