Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the latest date from a collection of objects using LINQ?

Tags:

I have a list of objects and each object has a ExpirationDate property which is of type DateTime. I want to retrieve the latest date in the list. Is there an elegant way of doing that through LINQ?

Something like:

DateTime latestDate =  myCollection.Where(r=>r.ExpirationDate.HasValue).MaxDate(r=>r.ExpirationDate.Value); 
like image 576
leora Avatar asked Jul 21 '11 11:07

leora


People also ask

How to get latest Date from a list in c#?

DateTime latestDate = myCollection. Where(r => r. ExpirationDate. HasValue) .

How do you select the record with the highest date in LINQ?

MaxBy() is now . MaxBy(). First() . This has the advantage that you can handle more than 1 result (used to be only the first person with the max age, but there may be more persons with the same age).

Does LINQ select return new object?

While the LINQ methods always return a new collection, they don't create a new set of objects: Both the input collection (customers, in my example) and the output collection (validCustomers, in my previous example) are just sets of pointers to the same objects.

What collections can LINQ be used with?

You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API.


2 Answers

DateTime? latestDate = myCollection.Max(r => r.ExpirationDate); 

Intellisense should have given you the Max() method. =)


Now, if you must assign it to a DateTime, I would consider doing thus:

DateTime latestDate = myCollection.Where(r => r.ExpirationDate.HasValue)                                   .Max(r => r.ExpirationDate)                                   .Value; 

This will not cover the case of an empty collection or a collection with only null ExpirationDates.


Depending on your earlier logic, checking

myCollection.Any(r => r.ExpirationDate.HasValue) 

might be a good idea before trying to assign a value.

like image 78
J. Steen Avatar answered Apr 13 '23 07:04

J. Steen


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.

like image 37
Bob Vale Avatar answered Apr 13 '23 07:04

Bob Vale