Home > DeveloperSection > Forums > LINQ query for retrieving data from list
Ankit Singh

Total Post:341

Points:2389
Posted on    August-18-2014 3:05 AM

 C# C# 
Ratings:


 1 Reply(s)
 604  View(s)
Rate this:

I have List collection of Message objects.

public class Message

{

    public int Id { get; set; }

    public string Body { get; set; }

    public string Sender { get; set; }

    public DateTime Timestamp { get; set; }

}

I want to get only one message with most recent Timestamp for each sender. How do I do it using LINQ?



Pravesh Singh

Total Post:411

Points:2881
Posted on    August-18-2014 3:29 AM

Hi Ankit, 
Try this:

var q = from n in table

        group n by n.Senderinto g

        select g.OrderByDescending(t=>t.Timestamp).FirstOrDefault();


Don't want to miss updates? Please click the below button!

Follow MindStick