Search code examples
linqif-statementuipathuipath-studio

How to check if dt.AsEnumerable is Null before copying it UIPATH


I am using

dt1.AsEnumerable().Where(
    Function(row) Not dt2.AsEnumerable().Select(
        Function(r) r.Field(Of String)("Otsikko")
    ).Any(
        Function(x) x = row.Field(Of String)("Otsikko")
    )
).CopyToDataTable()

Which works perfectly until Both A and B columns match. This leads to situation when "CopyToDataTable()" has nothing to copy and provides error.

How can I add IF to check if the item I am trying to copy is empty. If its not empty I want it to Copy the datatable, if it is the query should move forward.

I am newbie to all this! So any help is appreciated.

Thank you on advance.

Lari


Solution

  • Assuming I have understood the question properly, you have ran the query below which works fine except then the result is empty. This then fails as .CopyToDataTable() has nothing to copy.

    dt1.AsEnumerable().Where(
        Function(row) Not dt2.AsEnumerable().Select(
            Function(r) r.Field(Of String)("Otsikko")
        ).Any(
            Function(x) x = row.Field(Of String)("Otsikko")
        )
    ).CopyToDataTable()
    

    A solution would be to use a decision or if statement with the below code, this will return true if there is data in there, then you can use the above code to extract it, if it returns false, you can then handle this without an error getting thrown

    dt1.AsEnumerable().Where(
        Function(row) Not dt2.AsEnumerable().Select(
            Function(r) r.Field(Of String)("Otsikko")
        ).Any(
            Function(x) x = row.Field(Of String)("Otsikko")
        )
    ).Any()