Assuming you have a list containing the Person object, these objects have the dateofbirth attribute that can be empty. You may need to determine the earliest birth date.
One method is to find the minimum value of the Dateofbirth property with the min method. However, this will only return the minimum date value, not the actual Person object.
To get the corresponding object, you can use the Aggregate method:
The
var firstBorn = People.Aggregate((curMin, x) => (curMin == null || (x.DateOfBirth ?? DateTime.MaxValue) < (curMin.DateOfBirth ?? DateTime.MaxValue)) ? x : curMin);
In this example, the initial accumulator value is set to NULL. The function passed to the Aggregate method to check whether the current accumulator value is NULL, or the DateFbirth property of the current element (with DateTime.maxValue replace the NULL value) earlier than the current accumulator's DateFbirth property. If it is true, the function returns the current element as the new cumulator; otherwise, it returns the current accumulator.
The result of
Aggregate is the earliest Person object in birth. This more concise method does not need to perform a second query to retrieve the corresponding objects.The above is the detailed content of How to Find the Person with the Earliest Birthdate Using LINQ?. For more information, please follow other related articles on the PHP Chinese website!