16

I have the following LINQ in a WebAPI controller:

MyDate = i.Products.FirstOrDefault().Date

It works as expected. But, Products is a Collection, so there can be many dates. The above just selects the first one.

What I really want to do is to find the date, with the earliest time, and select that one.

How would that look?

1
  • .FirstOrDefault() can return null. Your code is vulnerable to NullReferenceExceptions when doing .Date. Review it for the case where the list of products is empty. Also take care with the answer you use, test it with an empty list - the same problem will most likely exist there.
    – ANeves
    Mar 31, 2016 at 14:06

8 Answers 8

21

If you only want the date and not the whole product, it's a little clearer to use Max or Min.

MyDate = i.Products.Select(x => x.Date).Max()

If you actually want the product, you'll need to sort by the date and then select the first one.

MyProduct = i.Products.OrderBy(x => x.Date).FirstOrDefault()
5
  • 4
    Ordering is an expensive operation and should not be used when not necessary. Your query for the product with the least recent date could be written as Product oldestProduct = i.Products.SingleOrDefault(x => x.Date == i.Products.Min(y=>y.Date)); or DateTime minDate = i.Products.Min(x => x.Date); Product oldestProduct = i.Products.SingleOrDefault(x => x.Date == minDate); Note: The latter version is more readable but needs two separate queries. Mar 31, 2016 at 11:06
  • 2
    @SørenD.Ptæus the cost of ordering depends on how the ordering is done. If it's accessing a database and there's an ordered index on the date then it would be pretty cheap.
    – Jon Hanna
    Mar 31, 2016 at 12:43
  • 1
    Take care: doing .Max() or .Min() on an empty collection will throw an exception!
    – ANeves
    Mar 31, 2016 at 14:04
  • 1
    @SørenD.Ptæus Beware that SingleOrDefault will thrown an exception if multiple items in the list have the same date value and it matches the Min value. Mar 31, 2016 at 16:32
  • 1
    @GalacticCowboy Good point. Although identical date values are quiet unlikely for time stamps I agree using SingleOrDefault leaves a chance for errors. If Date is not a time stamp but just a date without distinctive Ticks values or any other time information the scenario you describe is indeed a real issue and FirstOrDefault makes a much better option here. Apr 1, 2016 at 8:57
4

This way is more concise:

var earlyDate = i.Products.Min(p=>p.Date);

But you are sure that Product != null

2

You can use

 i.Products.OrderByDescending(x => x.Date).FirstOrDefault().Date;
3
  • This will give you the largest (recent) date
    – bit
    Mar 31, 2016 at 6:38
  • There is no OrderByAscending
    – bit
    Mar 31, 2016 at 6:40
  • Yeah, I just realized that
    – Alok Gupta
    Mar 31, 2016 at 6:40
2

Here is a lambda expression that will give you the minimum (earliest) date

DateTime earliestDate = i.Products.Min(p => p.Date);
2

Simple, if you need earliest use OrderBy and need oldest then use OrderByDescending.

 i.Products.OrderBy(x => x.Date).Select(x => x.Date).FirstOrDefault();
 i.Products.OrderByDescending(x => x.Date).Select(x => x.Date).FirstOrDefault();
1

Simply order by date:

MyDate = i.Products.OrderBy(x => x.Date).Select(x => x.Date).FirstOrDefault();
1

Optimized way would be :

i.Products.OrderByDescending(x => x.Date).Select(x => x.Date).FirstOrDefault();
1
i.Products.OrderBy(x => x.Date).FirstOrDefault().Date;

It has to be orderby and not orderbydescending. orderbydescending will give you the latest date first and order by will give you the earliest date first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.