Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the smallest date from a list of objects with a date?

Tags:

c#

.net

I created a simple class that represents a project:

public class EntityAuftrag
{
    public string cis_auftrag { get; set; }
    public string bezeich { get; set; }
    public DateTime? dStart { get; set; }
    public DateTime? dEnd { get; set; }
    public decimal? aufstunde { get; set; }
    public int id_auftrag { get; set; }
    public string barcolor { get; set; }
}

Now I have a list of these. I want to extract the smallest date, how do I do that?

like image 608
Luke Avatar asked Jul 26 '12 08:07

Luke


People also ask

How to get smallest Date from List in c#?

smallest = auftragList. Min(a => a. dStart);

What is Z in DateTime C#?

The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time.


1 Answers

You can use Enumerable.Min (null values will be ignored unless all values are null):

DateTime? smallest = auftragList.Min(a => a.dStart);

Edit: if you want to find the object with the earliest(start) date, you can use OrderBy and First:

EntityAuftrag auft = auftragList.OrderBy(a => a.dStart).First();

If you want the latest date, you can use Enumerable.OrderByDescending instead.

like image 102
Tim Schmelter Avatar answered Oct 10 '22 20:10

Tim Schmelter