Get Maximum Date using C# LINQ
This example gets maximum/highest date from list of dates.DateTime.Parse, is used to Parse Date Strings converts it into DateTime Object.
DateTime Custom Format:
yyyy is for four digit year
MMM for Month in Short form
dd is for Day .
void GetMaxDate()
{
String[] szDates = new String[] { "2013-jan-12",
"2013-Mar-12", "2013-Feb-12",
"2013-Apr-12", "2013-Oct-12","2013-Oct-12","2013-Nov-1",
"2012-DEC-31"};
var MaxDate = from dt in szDates
let custDt = DateTime.ParseExact(dt, "yyyy-MMM-%d",
System.Threading.Thread.CurrentThread.CurrentCulture)
orderby custDt.Date descending
select dt;
foreach(var u in MaxDate)
Console.WriteLine("{0}",u);
Console.WriteLine("Maxdate={0}",MaxDate.FirstOrDefault());
}
OUTPUT:
2013-Nov-1
2013-Oct-12
2013-Oct-12
2013-Apr-12
2013-Mar-12
2013-Feb-12
2013-jan-12
2012-DEC-31
Maxdate=2013-Nov-1
LINQ let clause allows us to store temporary results in query sequence evaluation.
Tags:Get Maximum Date using C# LINQ, LINQ Orderby on DateTime,LINQ LET CLAUSE,LINQ ORDERBY descending.How to get maximum date from selected dates.
No comments:
Post a Comment