Search code examples
c#.netlinqbindinglist

How to retrieve last object in sorted Binding list (.Net, LINQ)?


I have a BindingList with Comment objects inside. Comment contains DateTime and string

I need to order list by date (latest date last) and get string without making copies of BindingList nor affecting it's order.

How can such be accomplished?


Solution

  • To retrieve all of the strings, you can use:

    IEnumerable<string> theStringsInOrder = theBindingList.OrderByDescending(c => c.Date).Select(c => c.TheString);
    

    For the string associated with the "max" date:

    string latestString = theBindingList.OrderByDescending(c => c.Date).First().TheString;