LINQ query for retrieving data from list

Total Post:341


 1107  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?

  1. 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();


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.