How to filter DT if it contains no rows. Errors "source contains no DataRows"

I have a for each loop that checks will filter a Datatable if it exist on the Datatable. I did it like this

DT_FilteredMoney=in_DT_Money.AsEnumerable.Where(Function(x)x("Date").ToString.Equals(item.ToString)AND x("MoneyType").ToString.Equals("USD")).CopyToDataTable

I put an If statement after this that continuous the loop if the DT_FileteredMoney is Nothing or Rows.Count=0. Problem is, it errors before it reach this activity. Is there a better/right way of continuing a loop if the filtered DT is empty?Thank you.

1 Like

Hi @Shinjid

You can put an assign activity inside the if activity

in_DT_Money.AsEnumerable.Where(Function(x)x("Date").ToString.Equals(item.ToString)AND x("MoneyType").ToString.Equals("USD")).Count > 0

And if the count is greater than 0, then use the assign activity you are referring inside the then, else do nothing and continue with the loop.

Or of you want to put the assign before if activity,use assign inside the try catch activity.


1 Like

Should the code’s output be Boolean?

Hi @Shinjid

Please Check with Count in if condition then Proceed with CopyToDataTable

if Condition

DT_FilteredMoney=in_DT_Money.AsEnumerable.Where(Function(x)x("Date").ToString.Equals(item.ToString)AND x("MoneyType").ToString.Equals("USD")).Count>0

Then Proceed with

DT_FilteredMoney=in_DT_Money.AsEnumerable.Where(Function(x)x("Date").ToString.Equals(item.ToString)AND x("MoneyType").ToString.Equals("USD")).CopyToDatatable


1 Like


You are almost done

You can use the same expression but like this

DT_FilteredMoney=in_DT_Money.AsEnumerable.Where(Function(x)x(“Date”).ToString.Equals(item.ToString)AND x(“MoneyType”).ToString.Equals(“USD”)).CopyToDataTable.Rows.Count > 0

This will check that if number of rows is more than 0 which will give us a Boolean value as output

But I would suggest to use contains instead of equals in the expression
Because at times if the variable has extra space then EXACT method won’t work as it will expect the exact value getting matched

So use like this

DT_FilteredMoney=in_DT_Money.AsEnumerable.Where(Function(x)x(“Date”).ToString.Contains(item.ToString)AND x(“MoneyType”).ToString.Contains(“USD”)).CopyToDataTable.Rows.Count > 0

Cheers @Shinjid

1 Like

@Shinjid Yes it is Boolean


1 Like

I’ve tried, it works now. Thank you.

1 Like

I’ll try to contains too. I think I did this before but with filter data row instead. But it looks cleaner with LINQ so I stuck with it.

1 Like

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