Linq code

Bal_Amount.AsEnumerable().Where(Function(row) Not String.IsNullOrEmpty(row.Field(Of String)(“Enc Dt”)) AndAlso DateTime.TryParse(row.Field(Of String)(“Enc Dt”), New DateTime()) AndAlso DateTime.Parse(row.Field(Of String)(“Enc Dt”)) >= New DateTime(2023, 1, 1)).CopyToDataTable()—correct this code as i receive error Assign: Unable to cast object of type ‘System.DateTime’ to type ‘System.String’.

@Arvind1

Bal_Amount.AsEnumerable() _
    .Where(Function(row) Not IsDBNull(row("Enc Dt")) AndAlso row.Field(Of DateTime)("Enc Dt") >= New DateTime(2023, 1, 1)) _
    .CopyToDataTable()

Hi @Arvind1

Try this:

Bal_Amount.AsEnumerable().Where(Function(row) 
        Not String.IsNullOrEmpty(row.Field(Of String)("Enc Dt")) AndAlso 
        DateTime.TryParse(row.Field(Of String)("Enc Dt"), New DateTime()) AndAlso 
        DateTime.Parse(row.Field(Of String)("Enc Dt")) >= New DateTime(2023, 1, 1)).CopyToDataTable()

Hope it helps!!

Assign: Unable to cast object of type ‘System.String’ to type ‘System.DateTime’.

find this error

@Arvind1

Bal_Amount.AsEnumerable() _
    .Where(Function(row) _
        Not String.IsNullOrEmpty(row.Field(Of String)("Enc Dt")) AndAlso _
        DateTime.TryParse(row.Field(Of String)("Enc Dt"), Nothing) AndAlso _
        DateTime.Parse(row.Field(Of String)("Enc Dt")) >= New DateTime(2023, 1, 1)) _
    .CopyToDataTable()

Please try this linq query

Hi Parvathy
getting same error

Assign: Unable to cast object of type ‘System.DateTime’ to type ‘System.String’.

Hi @Arvind1

What is your date format?

Regards

07/29/22
08-01-2022
08/16/22
just like that

(From d in Bal_Amount.AsEnumerable
Let chk = isNothing(d("Enc Dt") OrElse String.IsNullOrEmpty(d("Enc Dt").toString.Trim)
Where Not chk
Let dv = d("Enc Dt").toString.Trim
Where DateTime.TryParse(dv, nothing)
Where DateTime.Parse(dv).Date >= (#01/01/2023#).Date
Select r = d).CopyToDataTable

Handling empty result:

We suggest to use the Field Extension method carefully as it relies on the calculated column value datatype

Hi rl gandu
getting same error
date format like that

07/29/22
08-01-2022
08/16/22

just analyze as described and incorporate in above LINQ the multi-format parsing

Hi @Arvind1

Try this:

Bal_Amount.AsEnumerable().Where(Function(row) 
        Not String.IsNullOrEmpty(row.Field(Of String)("Enc Dt")) AndAlso 
        IsValidDate(row.Field(Of String)("Enc Dt"), cultureInfo) AndAlso 
        DateTime.ParseExact(row.Field(Of String)("Enc Dt"), {"MM/dd/yy", "MM-dd-yyyy", "MM/dd/yy"}, System.Globalization.cultureInfo.InvariantCultture, System.Globalization.DateTimeStyles.None) >= New DateTime(2023, 1, 1)
    ).CopyToDataTable()

Regards

@Arvind1

Dim formats() As String = {"MM/dd/yy", "MM-dd-yyyy", "MM/dd/yyyy"}

filteredTable = Bal_Amount.AsEnumerable() _
    .Where(Function(row)
        Dim encDtStr = row.Field(Of String)("Enc Dt")
        Dim encDt As DateTime
        Return Not String.IsNullOrEmpty(encDtStr) AndAlso _
               DateTime.TryParseExact(encDtStr, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, encDt) AndAlso _
               encDt >= New DateTime(2023, 1, 1)
    End Function) _
    .CopyToDataTable()

Place this in invoke code

@ppr
Thanks Bro
Its very useful