Tuesday, 5 March 2013

Get Maximum Date using VB.NET LINQ


 Get Maximum Date using VB.NET 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 . %d is for single digit/double digit day.


  Private  Sub 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
                       Dim dt As select

            Dim u As var
            For Each u In MaxDate
           Console.WriteLine("{0}",u)
            Next

            Console.WriteLine("Maxdate={0}",MaxDate.FirstOrDefault())
End Sub


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 VB.NET LINQ, LINQ Orderby on DateTime,LINQ LET CLAUSE,LINQ ORDERBY descending.How to get maximum date from selected dates.