Count Items in a Datarow using Enumerable Function

Hi Folks,

I need to count the number of Items in a column Business Area and equate it to 1000.

If the value in business area column is anything other than 1000, I am getting error - The source contains no datarows.

Using below in If condition activity currently:

DT.Select(“[Business Area] = ‘1000’”). CopyToDataTable().Rows.Count > 0

Please suggest how to use the Enumerable function for this code snippet.

Thanks in advance :slight_smile:

You’re getting source contains no data rows because there are no rows with the value of 1000. So CopyToDataTable throws this error. It would be simpler to just use a Filter DataTable activity to filter into a temp dt, then check the rows count.

1 Like

Hey @shikharno.7 ,

YourDataTable.AsEnumerable().Count(Function(row) row(Of String)("Business Area") = "1000")

Try the above linq and check

1 Like

Try this link query

DT.AsEnumerable().Where(Function(row) row.Field(Of String)(“Business Area”) = “1000”).Any()

@shikharno.7

1 Like

For a count we would do:

myCount | int32 =

YourDTVar.AsEnumerable.Where(Function (x) x("Business Area").ToString.Trim.Equals("1000")).Count

If others is needed we would handle empty results similar like:

1 Like

Hi @shikharno.7

  • The error you’re encountering (“The source contains no datarows”) is occurring because the DT.Select("[Business Area] = '1000'").CopyToDataTable() expression tries to copy the filtered rows into a new DataTable, and if there are no matching rows, it raises an error.

To count the number of items in the “Business Area” column that have a value of 1000, you can use LINQ to directly count the matching rows without attempting to create a new DataTable. Here’s how you can do it:

DT.AsEnumerable().Count(Function(row) row.Field(Of String)(“Business Area”) = “1000”) = 1000

In this code

  • DT.AsEnumerable() converts your DataTable DT into an enumerable collection of DataRow objects.
  • .Count(Function(row) row.Field(Of String)("Business Area") = "1000") counts the rows where the “Business Area” column has a value of “1000.”
  • = 1000 checks if the count is equal to 1000.

This condition will be true if there are exactly 1000 rows with “Business Area” equal to 1000 in your DataTable. If the count is not 1000 or if any “Business Area” value is different from 1000, the condition will be false

1 Like

This is basically because of trying to find the rows which are EQUAL to 1000
And sometime if there is no record it will throw as no Datarow found and we won’t get the count as zero

For that first use a IF condition to validate whether it has any rows with value as 1000 with this expression in IF activity with condition like this

dt.AsEnumerable().Where(Function(row) row.Field(Of String)(“Business Area”).Equals(“1000”)).Any()

If true it goes to then block where use a assign activity like this to get the row count
int_rowcount = dt.Rows.Count

If it has no rows it goes to ELSE block where use a assign activity like this

int_rowcount = 0

Hope this helps

Cheers @shikharno.7

1 Like

Hi,

Please use below query.

DT.AsEnumerable().Where(Function(row) row.Field(Of String)(“Business Area”) = “1000”).Count() > 0

1 Like

I am receiving the below error when copying the code

image

Hi @ppr , how will this statement change if, we need to check for two columns instead of just one here being Business Area. Say another Column is “ID”.

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