views:

49

answers:

1

My particular example is fairly complex but I think the concept would apply equally to something like a logging system so I'll use that instead for ease of explanation. It's a ficticious example, please don't harp on or agonise over what is achitectually, programatically or morally wrong with the example itself :)

Say you have this:

class LogEntry
{
    int ID;
    int UserName;
    datetime TimeStamp;
    string Details;
}

and you have pulled a set of data like this:

ID  Username   Timestamp   Details
1   foo        1/01/2010   Account created
2   zip        2/02/2010   Account created
3   bar        2/02/2010   Account created
4   sandwich   3/03/2010   Account created
5   bar        5/05/2010   Stole food
6   foo        5/05/2010   Can't find food
7   sandwich   8/08/2010   Donated food
8   sandwich   9/09/2010   Ate more food
9   foo        9/09/2010   Ate food
10  bar        11/11/2010  Can't find food

What I want to do is select only the last single record (ie Sort on TimeStamp Descending) for each user (ie GroupBy Username). I can get my head around Distinct, and to a lesser extent GroupBy, but combining all of these in a single statement which also returns the non-distinct/grouped fields/properties AND sorts by timestamp is giving me a headache.

What should come out with the above example is:

ID  Username   Timestamp   Details
2   zip        2/02/2010   Account created
8   sandwich   9/09/2010   Ate more food
9   foo        9/09/2010   Ate food
10  bar        11/11/2010  Can't find food

I don't want to 'cheat' and resort to a long-winded way of doing it when performance isn't critical here and I'm moderately confident it can be done in a single LINQ statement.

+3  A: 

Hopefully my Linq-fu is right on this one: =)

var results = sourceList
    .OrderByDescending(item => item.Timestamp)
    .GroupBy(item => item.Username)
    .Select(grp => grp.First())
    .ToArray();

This sample code using your data, and final ordering by ID, gives exactly the same output as your example: (if you don't mind the crude formatting!)

class Program
{
    static void Main(string[] args)
    {
        var sourceItems = new[] {
            new LogEntry {ID=1   ,UserName="foo      ", TimeStamp= new DateTime(2010 ,1,01),Details="Account created ",}    ,
            new LogEntry {ID=2   ,UserName="zip      ", TimeStamp= new DateTime(2010 ,2,02),Details="Account created ",}    ,
            new LogEntry {ID=3   ,UserName="bar      ", TimeStamp= new DateTime(2010 ,2,02),Details="Account created ",}    ,
            new LogEntry {ID=4   ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,3,03),Details="Account created ",}    ,
            new LogEntry {ID=5   ,UserName="bar      ", TimeStamp= new DateTime(2010 ,5,05),Details="Stole food      ",}    ,
            new LogEntry {ID=6   ,UserName="foo      ", TimeStamp= new DateTime(2010 ,5,05),Details="Can't find food ",}    ,
            new LogEntry {ID=7   ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,8,08),Details="Donated food    ",}    ,
            new LogEntry {ID=8   ,UserName="sandwich ", TimeStamp= new DateTime(2010 ,9,09),Details="Ate more food   ",}    ,
            new LogEntry {ID=9   ,UserName="foo      ", TimeStamp= new DateTime(2010 ,9,09),Details="Ate food        ",}    ,
            new LogEntry {ID=10  ,UserName="bar      ", TimeStamp= new DateTime(2010,11,11),Details="Can't find food ",}    ,
        };

        var results = sourceItems
            .OrderByDescending(item => item.TimeStamp)
            .GroupBy(item => item.UserName)
            .Select(grp => grp.First())
            .OrderBy(item=> item.ID)
            .ToArray();

        foreach (var item in results)
        {
            Console.WriteLine("{0} {1} {2} {3}",
                item.ID, item.UserName, item.TimeStamp, item.Details);
        }
        Console.ReadKey();
    }
}


public class LogEntry
{
    public int ID;
    public string UserName;
    public DateTime TimeStamp;
    public string Details;
}
Will
Looks good to me. Though it appears to be sorted by `ID` in the end.
Jeff M
Legend! Thanks. It's frustrating how hard it is to find simple examples that aren't >overly< simple.
FerretallicA
@Jeff: Cheers - I've asked the OP to clarify this point.
Will
ID ordering is incedental and not so important. The first answer was perfect.
FerretallicA