Search This Blog

Thursday, January 30, 2014

LINQ DataTable compare date VB.NET

VB.NET > Data > DataTable > Filter with LINQ

LINQ DataTable compare date VB.NET example

   Dim dt As DataTable = New DataTable("table")
        dt.Columns.Add("id", Type.GetType("System.Int32"))
        dt.Columns.Add("name", Type.GetType("System.String"))
        dt.Columns.Add("date_of_birth", Type.GetType("System.DateTime"))
 

        Dim dr As DataRow = dt.NewRow()
        dr("id") = 1
        dr("name") = "john"
        dr("date_of_birth") = #1/1/2000#
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("id") = 2
        dr("name") = "dan"
        dr("date_of_birth") = #9/11/1973#
        dt.Rows.Add(dr)

        Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
                            Where n.Field(Of Date)("date_of_birth") = #9/11/1973#
                    Select n).CopyToDataTable()