I've been using the recent C# and .NET stuff pretty heavily in a project for work. In particular, I've been using the LINQ and new lambda syntax quite a bit. I like it, because if I'm just selecting some subset of elements or doing any kind of query, I can almost always get away without temporary variables or a foreach loop.
But today I hit a (minor) snag. I have a collection of things. I want to find the thing with the minimum timestamp. Previously, the surrounding code didn't need to know the object itself, so I was just doing this:
DateTime oldestTime = Collection.Min(obj=>obj.Timestamp);
Now I need to get the object itself. Obviously, I can easily write a foreach loop to find the oldest object. Or I could write a new extension method for IEnumerable and IQueryable to add to the huge set of operations provided by LINQ. But before I do that... am I missing some really obvious way to do this with the existing operations? Min always returns the value of the thing being compared. The only way I can see to implement this using the existing functions would be:
MyObject oldestObject = Collection.OrderBy(obj => obj.Timestamp).First();
This does what I want, but forces me to pay the price of an unnecessary sort on the collection. Before anyone asks, keeping the collection sorted by Timestamp isn't really an option ... it's queried in lots of different ways so I wouldn't really be solving anything by doing that.
So, is there some other function that I've overlooked that would do what I want without the extra cost of sorting?