Convert IF to mainDT.Select

I have a For Each Row in Datatable that’s looking for the first row that matches an IF condition, and adds it to another datatable (then breaks because I only need the first matching row). Looking to simplify all this.

Inside the For Each Row in Datatable the IF condition is:

(NOT CurrentRow(“CREATED_IN_NCINO”).ToString.ToUpper = “YES”) AndAlso DateDiff(DateInterval.Day,Now,DateTime.ParseExact(CurrentRow(“Maturity Date”).ToString,“M/d/yyyy”,System.Globalization.CultureInfo.InvariantCulture)) = 34

I got this far, where the expression didn’t give me any design errors, but when executing I got “String was not recognized as a valid datetime”

Assign loansDT = mainDT.Select(“CREATED_IN_NCINO != ‘YES’ AndAlso '” + DateDiff(DateInterval.Day,Now,DateTime.ParseExact(“Maturity Date”.ToString,“M/d/yyyy”,System.Globalization.CultureInfo.InvariantCulture)).ToString + “’ = ‘34’”).Take(1).CopyToDataTable

You may use “Log Message” or “Message Box” activity to check the value in column “Maturity Date”. The format of the date string may be different from what you think.
CurrentRow(“Maturity Date”).ToString


In this case, it may be better to use LINQ because we can directly use any function including DateDiff in it.

arrDr = mainDT.AsEnumerable.Where(Function(r) r("CREATED_IN_NCINO").ToString<>"YES" AndAlso  DateDiff(DateInterval.Day,Now,DateTime.ParseExact(r("Maturity Date").ToString,"M/d/yyyy",System.Globalization.CultureInfo.InvariantCulture)).ToString = "34").ToArray

Then check arrDr.Any

loansDT = arrDr.CopyToDataTable


loansDT = arrDr.Take(1).CopyToDataTable


1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.