Dear friends,
I am stuck at a point please help me
I am using dtstatement.AsEnumerable().where(Function (r) not ((Datetime.parseexact(r(“Closed Date”). ToString.trim(),“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture)<(Datetime.parseexact(formattedstartdate,“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture))))). copytodatatable
After this filtering if there is no data bot is throwing error how to handle this.
Please help me
mkankatala
(Mahesh Kankatala)
August 9, 2024, 2:30pm
3
Hi @0bb4628e217fd43ac86ac9294
Check the below expression to handle the exception, create a datatable variable to store the Output data called Output_DT,
- Assign -> Output_DT = If(dtstatement.AsEnumerable().any(Function (r) Not ((Datetime.parseexact(r("Closed Date"). ToString.trim(), "dd-MM-yyyy", System.Globalization.Cultureinfo.invariantculture)<(Datetime.parseexact(formattedstartdate, "dd-MM-yyyy", System.Globalization.Cultureinfo.invariantculture))))), dtstatement.AsEnumerable().where(Function (r) Not ((Datetime.parseexact(r("Closed Date"). ToString.trim(), "dd-MM-yyyy", System.Globalization.Cultureinfo.invariantculture)<(Datetime.parseexact(formattedstartdate, "dd-MM-yyyy", System.Globalization.Cultureinfo.invariantculture))))). copytodatatable, New System.Data.Datatable())
After filtration data is there then it will store the Filtered data else Output_DT will be empty datatable.
Hope it helps!!
ppr
(Peter Preuss)
August 9, 2024, 2:34pm
4
Also we can simplify and optimize the statement:
Shortening the statement by namespace import:
Avoiding startdate parsing for each loop by using a variable:
Assign Activity =
StartDate | DataType: DateTime =
Datetime.ParseExact(formattedstartdate,"dd-MM-yyyy", Cultureinfo.InvariantCulture)
Assign Activity:
Result | List(Of DataRow) =
(From d in dtstatement.AsEnumerable().
Let dp = Datetime.ParseExact(d("Closed Date").ToString.Trim(), "dd-MM-yyyy",Cultureinfo.InvariantCulture)
Where dp.Date < StartDate.Date
Select r=d).ToList()
If Activtiy: Result.Any()
Then …
Else …
As described in the firstAid
About LINQ:
[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum
@0bb4628e217fd43ac86ac9294 the easiest way to handle this is, use if statement. let me show how you can use
Assign activity
dtresult =
if (dtstatement.AsEnumerable().where(Function (r) not ((Datetime.parseexact(r(“Closed Date”). ToString.trim(),“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture)<(Datetime.parseexact(formattedstartdate,“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture))))).count > 0 , dtstatement.AsEnumerable().where(Function (r) not ((Datetime.parseexact(r(“Closed Date”). ToString.trim(),“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture)<(Datetime.parseexact(formattedstartdate,“dd-MM-yyyy”, System.Globalization.Cultureinfo.invariantculture))))).copytodatatable , nothing )
This will check if your expression contains datarows or not; if it contains datarows, it will copy them to datatable, else it will assign nothing.
After this, you can use if activity to check if dtresult isnot nothing , you can continue your process.
I hope this helps.