CodeStageGen

Filter a Blue Prism collection without a loop (DataView and LINQ)

Updated 25 September 2026 · Every C# sample here compiles with the compiler that ships with Blue Prism.

The usual way to filter a collection on the Blue Prism canvas is a Loop stage, a Decision on each row, and Calculation or Collection stages that copy the matching rows somewhere else. It works, but it's four or five stages per row. On a big collection that's slow, it fills the session log, and the next developer has to trace it by hand.

In a Code Stage a collection is a System.Data.DataTable, and .NET can filter a DataTable in one step. There are two good ways to do it.

Option 1: DataView.RowFilter (no extra references)

DataView is part of System.Data, which every object already references. Give the Code Stage an input Items (Collection) and an output Open Items (Collection):

C#
DataView view = new DataView(Items);
view.RowFilter = "Status = 'Open'";
Open_Items = view.ToTable();

That's the whole stage. The output keeps the input's columns and holds only the rows that match.

RowFilter syntax you'll actually need

  • Text values go in single quotes: Status = 'Open'.
  • Column names with spaces or symbols go in square brackets: [Invoice Number] = 'INV-1'.
  • Combine conditions with AND, OR and NOT, and use brackets to group them.
  • Wildcards use LIKE with % or *: [Invoice Number] LIKE 'INV-%'.
  • Lists: Status IN ('Open', 'Pending').
  • Empty cells: ISNULL(Status, '') = '' finds rows where Status is empty or null.

Taking the filter value from an input

If the value comes from an input, escape single quotes, or a name like O'Brien breaks the filter:

C#
DataView view = new DataView(Items);
view.RowFilter = string.Format("Status = '{0}' AND Amount > 1000", Status.Replace("'", "''"));
Open_Items = view.ToTable();

Numbers stored as Text

A Number field in a Blue Prism collection arrives as a decimal column, so comparisons on it behave. But collections read from Excel or CSV often hold numbers as Text, and then three things go wrong:

  • Quoting the number compares text. Amount > '1000' returns 1500, but also 200 and 900, because as text "9" sorts after "1". Leave numbers unquoted: Amount > 1000 converts each cell and compares numbers.
  • One badly formatted cell stops the stage. The unquoted comparison only works while every cell parses as a number. A value like 1,250.00 or N/A throws Cannot perform '>' operation on System.String and System.Int32.
  • Sorting is alphabetical. view.Sort = "Amount DESC" on a Text column puts 900 above 1500.

When the column is Text and the values are messy, use LINQ (below) and convert each value yourself with an explicit culture. That handles 1,250.00 correctly and sorts numerically.

Case sensitivity

DataTable comparisons ignore case by default, so Status = 'open' matches "Open" and "OPEN". If case matters, set Items.CaseSensitive = true; before you create the view.

Option 2: LINQ (for conditions RowFilter can't express)

RowFilter can't call .NET methods. When the condition needs one (a regex, a date calculation, parsing), use LINQ:

C#
var rows = Items.AsEnumerable()
    .Where(r => r.Field<string>("Status") == "Open"
             && Convert.ToDecimal(r["Amount"], CultureInfo.InvariantCulture) > 1000m);
Open_Items = rows.Any() ? rows.CopyToDataTable() : Items.Clone();

Sorting Text numbers numerically works the same way:

C#
var sorted = Items.AsEnumerable()
    .OrderByDescending(r => Convert.ToDecimal(r["Amount"], CultureInfo.InvariantCulture));
Sorted_Items = sorted.Any() ? sorted.CopyToDataTable() : Items.Clone();

LINQ over a DataTable needs more setup than DataView. In Code Options on the Initialise page:

  • External References: System.Core.dll and System.Data.DataSetExtensions.dll
  • Namespace Imports: System.Linq and System.Globalization

Without System.Data.DataSetExtensions.dll you get: 'DataTable' does not contain a definition for 'AsEnumerable' and no accessible extension method 'AsEnumerable' accepting a first argument of type 'DataTable' could be found.

Two details in that snippet matter:

  • CopyToDataTable() throws when no rows match. Items.Clone() returns an empty table with the same columns instead, so the stage never fails just because nothing matched.
  • Field<string> expects a Text column. On a Number column use Field<decimal>, or read r["Amount"] and convert it as shown above. A cell that isn't a number at all (N/A) still throws, so clean the data or skip such rows first.

Which should you use?

DataView.RowFilterLINQ
Extra referencesNoneSystem.Core.dll, System.Data.DataSetExtensions.dll
ConditionsComparisons, AND/OR, LIKE, IN, ISNULLAnything C# can express
Readability for non-codersFilter reads like SQLNeeds C# knowledge

Start with RowFilter. Move to LINQ only when the condition genuinely needs code.

Related

More Code Stage guides