none
how to write linq query with order by desc date (latest datetime) RRS feed

  • Question

  • i want to get the latest records of each group.how to add orderby updatedtime desc in select query

     var wt = from w in DB.watertowerlevels                         group w by w.towerid into g                                                                       select new                         {                             towerid = g.Key,                             waterlevel = g.FirstOrDefault().waterlevel,                             updatedtime = g.FirstOrDefault().updatedtime.ToString()                         };

    Wednesday, November 23, 2016 2:05 PM

All replies

  • Hi krishna mohan p v,

    You could OrderByDescending method, like this:

    var wt = from w in DB.watertowerlevels                         
    
      group w by w.towerid into g    
    
         select new           
    
      {      
    
         towerid = g.Key,                            
    
        waterlevel = g.FirstOrDefault().waterlevel,                             
    
        updatedtime = g.OrderByDescending(t=>t.updatedtime).FirstOrDefault().updatedtime.ToString()                         
    };
    
    

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 24, 2016 2:51 AM
    Moderator