Finding Min or Max Date using LINQ to SQL

In one of my recent project, I had to retrieve Minimum and Maximum date from the result set using LINQ to SQL. I was really surprised how easy it was:

//Retrieve Minimum Date

var MinDate = (from d in dataRows select d.Date).Min();

 

//Retrieve Maximum Date

var MaxDate = (from d in dataRows select d.Date).Max(); 

In its simplest form, all you have to do is have .Min() or .Max() at the end of your LINQ query. That’s it.