If condition better approach

Hi

I have a sequence where have a counter variable declared =0
success = 0
failure = 0

my flow ->Try block (read range → for each → then 2 if conditions) catch block empty

i have two if conditions : one to check string = success in the current row of a dtTable coming
if it finds success then my counter = success +1

another if does same thing for string = failure in the current row of a dtTable coming
if it finds failurethen my counter = failure +1

but i have more then 1lakh rows

i want the final counter of answers to be stored and sent to my client !!

for example: success items= 29 and fail items = 9

but what happens when the column has no value for more then 70thousand lines and the log message keeps on prinitingthe counter values for them

can someone guide me what is the efficient way here ?

@samantha_shah

place the second if activity in the else block of first if activity so that it if it is not success, it will check for the failure one.

@samantha_shah

Hope you need to take the Success and Failure count from the column “Status” in a data table. If so please check the following 2 approaches. You can go either of it.

  1. Use 2 filter data table activity. One for success and other for failure. From the filtered data table u can get the rows count using —> dtname.rows.count.tostring
  2. You can use the assign variable name= DTname.Select(“Status = ‘Success’”).Length.ToString and the same way for failure status.

Let me know if you need any clarification.

Hi @samantha_shah

Your data is in the datatable, success and failure is in the column and you want to get the Count of Status rows and Failure rows.

If this is your case, then you can use the LINQ expressions to get the Count and store in a Variables.

- Assign -> Success_Count = dt.AsEnumerable().Count(Function(X) X("Column name").Equals("Success"))

- Assign -> Failure_Count = dt.AsEnumerable().Count(Function(X) X("Column name").Equals("Failure"))

Note : Success_Count and Failure_Count are the Int32 datatype variables and change the column name in the both LINQ Expressions.

Hope it helps!!

1 Like

Hi,

If you want to count number of rows which has “Success”, the following will work.

dt.AsEnumerable.Count(Function(r) r("Status").ToString()="Success")

Or if you need to do some process in the loop, dictionary will help you as the following.

dict = New Dictionary(Of String,Int32)From {{"Success",0},{"Failure",0}}

Then

dict(CurrentRow("Status").ToString()) = dict(CurrentRow("Status").ToString()) +1

dict(“Success”) and dict(“Failure”) will return sum value for each item.

note: It’s unnecessary to use IF

Regards,

1 Like

Hi @samantha_shah . Use 2 Filter datatable activities with success and Failure condition match and then get rowcount of each table. Activities - Filter Data Table

1 Like

Hi @samantha_shah

How about this

  1. Initialize two integer variables, let’s call them totalCount and noValueCount, to 0 before the loop.
  2. Inside the loop, add a condition to check if the value in the current row’s “Status” column is empty or null.
  • If it’s empty/null, increment noValueCount.
  • If it’s not empty/null, check for “Success” or “Failure” conditions and increment the respective counters (Success_Count or Failure_Count).
  1. After the loop completes, increment totalCount by adding Success_Count and Failure_Count.
  2. Report the final counts to your client:
  • Total rows processed: totalCount
  • Rows with “Success”: Success_Count
  • Rows with “Failure”: Failure_Count
  • Rows with no value: noValueCount
1 Like

Hi @PaviS

i am trying to learn this activity filter data table

took two activities
can you please tel me how can i get the column name here ? and what alterations will i require in the activity to give me the following output you mentiones

please let me know , ican read somewhere as well if you are busy and i can learn this

thanks

Instead of doing all this, use the LINQ Expressions which is simpler and you can directly use those in assign activity, check the above response,

If you need any further assistance needed let me know… @samantha_shah

Hi @samantha_shah

At last in DT you can use Linq for it to get the count

DT.AsEnumerable.Count(Function(x) x("Status").ToString.Equals("Success"))


DT.AsEnumerable.Count(Function(x) x("Status").ToString.Equals("Failure"))

Hope it will helps you :slight_smile:
Cheers!!

Hi @mkankatala

thanks for further ideas

the reason i didnt too LINQ expressin because i had no clue what it is

for example: what is function(X)and how does it work

please help me understand it will be helpful

Okay @samantha_shah

You can simply use the LINQ Expression in assign activity.

  1. dt.AsEnumerable(): This converts the DataTable dt into an enumerable collection of DataRow objects. This allows us to use LINQ queries to manipulate and query the data in the DataTable.
  2. .Count(): This LINQ extension method counts the number of elements in the sequence that satisfies a given condition.
  3. Function(row): This is a lambda function that takes a DataRow (row) as input.

Hope you understand!!

1 Like

thanks for the clarification it helped me

@mkankatala

1 Like

It’s my pleasure… @samantha_shah

Happy Automation!!

1 Like

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