开发者

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

开发者 https://www.devze.com 2023-03-22 11:47 出处:网络
I have a list of objects and each object has aExpirationDate 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?

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.Expi开发者_JAVA百科rationDate.HasValue).MaxDate(r=>r.ExpirationDate.Value);


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.


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.


Just make sure there is a NullReference check before assigning .ExpriationDate:

DateTime maxDate = myCollection.OrderByDescending(o => o.ExpirationDate).Take(1).FirstOrDefault().ExpirationDate;

Bob Vale had the right idea with

DateTime maxDate = objL.Max(o => o.ExpirationDate);


Sort the list by the date descending and take the first (projecting if necessary).

var latestExpirationDate = myCollection
    .Where(item => item.ExpirationDate.HasValue)
    .OrderByDescending(item => item.ExpirationDate)
    .Select(item => item.ExpirationDate) // DateTime?
    .FirstOrDefault();
if (latestExpirationDate != null)
{
    // do something with the value stored in latestExpirationDate
}

As I wrote this, I was under the impression that the Max() method only operated on numeric types (i.e., int, short, decimal, etc.). It appears to work with DateTime objects too. So this could work as well:

var expiredItems = myCollection
    .Where(item => item.ExpirationDate.HasValue);
if (expiredItems.Any())
{
    var latestExpirationDate = expiredItems.Max(item => item.ExpirationDate.Value);
    // do something with latestExpirationDate
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号