CodeStageGen

Remove duplicate rows from a Blue Prism collection

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

Duplicate rows turn up all the time: a report exported twice, a work queue loaded from two sources, an invoice listed once per line item. Removing them on the canvas means a nested loop or a temporary collection and a lot of Decision stages. In a Code Stage it's a few lines.

Every example below takes an input Items (Collection) and returns an output Unique Items (Collection). A Blue Prism collection is a System.Data.DataTable inside a Code Stage, so these are ordinary .NET DataTable operations.

Whole-row duplicates

Rows count as duplicates when every column matches. DataView.ToTable can return only the distinct rows:

C#
string[] columns = Items.Columns.Cast<DataColumn>()
    .Select(col => col.ColumnName)
    .ToArray();
Unique_Items = Items.DefaultView.ToTable(true, columns);

The true means "distinct". Passing every column name keeps the output's columns the same as the input's. This needs System.Linq in Namespace Imports and System.Core.dll in External References, for Cast and Select.

The trap: passing one column name

It's tempting to write Items.DefaultView.ToTable(true, "Invoice Number") to de-duplicate by invoice number. That returns the distinct invoice numbers and nothing else: the output has a single column, and every other field is gone. To de-duplicate by a key and keep the whole row, use the next approach.

Duplicates by a key column, keeping the first row

This keeps the first row for each invoice number and drops the rest, with all columns intact:

C#
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
DataTable result = Items.Clone();
foreach (DataRow row in Items.Rows)
{
    string key = Convert.ToString(row["Invoice Number"]).Trim();
    if (seen.Add(key))
        result.ImportRow(row);
}
Unique_Items = result;
Duplicates_Removed = Items.Rows.Count - result.Rows.Count;

How it works:

  • Items.Clone() copies the structure (columns and types) with no rows.
  • seen.Add(key) returns true only the first time a key appears, so later rows with the same key are skipped.
  • ImportRow copies the whole row into the result.
  • Duplicates_Removed is an extra Number output: how many rows were dropped. It's useful for logging, or for a Decision that flags unexpected duplicates.

A HashSet lookup takes the same time however many keys it already holds, so this stays fast on collections with tens of thousands of rows. A nested loop that compares every row with every other row does not.

Duplicates across several columns

When a duplicate means "same invoice number and same amount", join the key columns into one key:

C#
string[] keyColumns = { "Invoice Number", "Amount" };
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
DataTable result = Items.Clone();
foreach (DataRow row in Items.Rows)
{
    string key = string.Join("|", keyColumns.Select(k => Convert.ToString(row[k]).Trim()));
    if (seen.Add(key))
        result.ImportRow(row);
}
Unique_Items = result;

Pick a separator (| here) that can't appear in the data. Otherwise "A|B" + "C" and "A" + "B|C" would count as the same key.

Case and whitespace: decide on purpose

The two approaches disagree about what "the same" means:

ValuesToTable(true, ...)HashSet with OrdinalIgnoreCase and Trim()
INV-1 and inv-1Different rowsDuplicates
INV-1 and INV-1 (trailing space)Different rowsDuplicates

For identifiers typed by people or pasted from emails, you almost always want the HashSet behaviour. If case really matters, use StringComparer.Ordinal and drop the Trim().

Keeping the last row instead of the first

Loop from the bottom so the latest row wins, then restore the original order:

C#
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var keep = new List<DataRow>();
for (int i = Items.Rows.Count - 1; i >= 0; i--)
{
    DataRow row = Items.Rows[i];
    if (seen.Add(Convert.ToString(row["Invoice Number"]).Trim()))
        keep.Add(row);
}
keep.Reverse();
DataTable result = Items.Clone();
foreach (DataRow row in keep)
    result.ImportRow(row);
Unique_Items = result;

Related

More Code Stage guides