Help with if/then/else Logic using LINQ

I am trying to utilize LINQ to speed up the processing of a table that I have. I can get the expression to pull just one scenario, but not all that are required.


I have a table similar to the one above, but I’ve simplified it for this question. Date1 correlates with Thing1, Date2 correlates with Thing2, and Date3 correlates withThing3.

For each Employee I need to make sure that if Thing1 is true, then Date1 has a date. If Thing1 is false, then Date1 should not have a date.

Same applies for Date2/Thing2 and Date3/Thing3

Each date/thing discrepancy that doesn’t meet the above criteria would be exported to a new data table.

Thanks in advance and let me know if clarification is needed.

1 Like


What is your expected result? Is it filtered datatable? If so, the following will work

dict =New Dictionary(Of String,String)From{{"Date1","Thing1"},{"Date2","Thing2"},{"Date3","Thing3"}}


arrDr = dt.AsEnumerable.Where(Function(r) dict.Any(Function(kv) not DateTime.TryParse(r(kv.Key).ToString,New DateTime)=CBool(r(kv.Value)))).ToArray() (8.8 KB)


1 Like

Hi @mbuehler

Give a try to this.

dt_input.AsEnumerable.Where(Function(x) Not( ((x("Thing1").ToString.Equals("TRUE") And Not String.IsNullOrEmpty(x("Date1").ToString)) Or (x("Thing1").ToString.Equals("FALSE") And  String.IsNullOrEmpty(x("Date1").ToString))) And ((x("Thing2").ToString.Equals("TRUE") And Not String.IsNullOrEmpty(x("Date2").ToString)) Or (x("Thing2").ToString.Equals("FALSE") And  String.IsNullOrEmpty(x("Date2").ToString))) And ((x("Thing3").ToString.Equals("TRUE") And Not String.IsNullOrEmpty(x("Date3").ToString)) Or (x("Thing3").ToString.Equals("FALSE") And  String.IsNullOrEmpty(x("Date3").ToString)))) ).CopyToDataTable

Sample Workflow:
Sequence6.xaml (9.1 KB)

Hope this might help you.

(From d in YourDataTableVar.AsEnumerable
Let cs1 = new String(){"Date1","Date2","Date3"}
Let cs2 = new String(){"Thing1","Thing2","Thing3"}
Let dpc = cs1.Select(Function (x) DateTime.TryParse(d(x).toString.Trim, nothing)).toArray
Let tbc = cs2.Select(Function (y) CBool(d(y).toString.Trim)).toArray
Let chk = dpc.SequenceEqual(tbc)
Where chk
Select r = d).CopyToDataTable
  • cs1, cs2 could be externalized so it will not be done for each loop
  • the dpc, tbc constructions can be modified if needed e.g DateTimeParseExact…

Handling empty results we can do as described here:
:ambulance: :sos: [FirstAid] Handling of The source contains no DataRows exception - News / Tutorials - UiPath Community Forum

Thank you all for your possible solutions, however I will no longer require a solution to this as the scope of the project changed, directly impacting the need for this.

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