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?
.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.