LINQ QUERY FOR RETRIEVING DATA FROM LIST

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C# 
C#
 800  View(s)
Ratings:
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?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: LINQ query for retrieving data from list

    Hi Ankit, 

    Try this:

    var q = from n in table

            group n by n.Senderinto g

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!