You are almost there. Use Max:
DateTime? biggest = myCollection.Max(r=>r.ExpirationDate);
If all expiration dates are null or the collection is empty you will get Null as a result, otherwise the greatest date.
As you have commented on J. Sheens' answer that you want an DateTime as a result you will need to do something about any empty collection or no items with a value, you could do this with
DateTime biggest=myCollection.Max(r=>r.ExpirationDate) ?? DateTime.MinValue
This will give you DateTime.MinValue
instead of nulls in my previous example (it also has the advantage over using the any
clause that it iterates the collection once). I picked MinValue
as an example. You could use your own abitary date.
Using the DateTime? is better, because it allows you to use null for what it's meant to mean - undefined as MinValue
could be a valid item in your list.
ExpirationDate.HasValue
andExpirationDate.Value
indicating that ExpirationDate is of typeDateTime?
(ieNullable<DateTime>
)