cancel
Showing results for 
Search instead for 
Did you mean: 

Unique Items in a collection

OliverMohajeri
Level 4

Does anyone have an existing activity or some VB code I can use to filter a collection to only include unique items?

Thank you :)



------------------------------
Oliver Mohajeri
Consultant
EY
Europe/Zurich
------------------------------
8 REPLIES 8

ŁukaszJaniak
Level 3
Hi, to get some unique values You can loop through the collection and for each value filter the collection to find values that occur only once, then just copy the value to the new collection with unique values.

------------------------------
Łukasz Janiak
RPA Developer
Nordea
Europe/Łódź
------------------------------

david.l.morris
Level 15
Hi Oliver,

I made an action in BP that does not use any custom code stage, but it's a bit convoluted because I have it accounting for any collection of any number of fields and gives the choice to deduplicate based on one or more fields. This kind of manipulation is likely better in code as you mentioned. But, to solve at least your use case, it's not very difficult to do in BP.
First, I'll assume you're trying to filter a collection for unique values in a single field. The most efficient way that I am aware of is to first use Sort Collection on the field you are focusing on. Then, use Loop stages to loop through the collection. Also, create two data items: Current Value and Previous Value.
Here's the flow or at least this is something like what you'd go for.: (Collection1 is your main collection and Collection2 is what you're copying unique values to)
Sort Collection1 on your field so that equal values are next to each other alphanumerically.
Loop Start
Get the current value of the field and store it in 'Current Value'
Decision stage: [Current Value]=[Previous Value]. (FYI: the first time through the loop it will compare the current value to empty text -- if this is a text datatype)
  If Yes, Go to the Loop End
  If No, add a new row to Collection2 and store the current row in Collection1 into the new row in Collection2
  (still under If No) Also set the value in [Current Value] into [Previous Value]
Loop End

If you are trying to compare the entire row, there are a couple ways to do this. You can compare entire collections like [CollectionA]=[CollectionB] so you'd probably copy rows out into temporary, single row collections to compare them to each other. Otherwise, depending on the datatypes of your fields, you could compare a concatenation of all the fields, like [CollectionA.Field1] & [CollectionA.Field2] = [CollectionB.Field1] & [CollectionB.Field2].

------------------------------
Dave Morris
3Ci @ Southern Company
Atlanta, GA
------------------------------

Dave Morris, 3Ci at Southern Company

AmiBarrett
Level 12

Here's some C# code I wrote to pull distincts. Should be easy enough to convert back to VB.

Inputs:
Collection - Collection
Column - Text

Outputs:
Sorted Collection - Collection

DataView dv = Collection.DefaultView;
Sorted_Collection = dv.ToTable(true, Column);


------------------------------
Ami Barrett
Lead RPA Software Developer
Solai & Cameron
America/Chicago
------------------------------

OliverOng
Level 2
Hi Oliver,

As previously mentioned, it's relatively trivial to create an object to loop through the input collection to retrieve unique values. I've included an action I created for this very purpose though I wouldn't suggest using this on large collections.


------------------------------
Oliver Ong
Consultant
Ernst & Young LLP
America/Edmonton
------------------------------

To expand on Amy's suggestion, you can specify which columns you want as unique, like this.

Dim v As New DataView(Collection_In)
Collection_Out = v.ToTable(true, Column_Name_CSV.Split(","))

Note however that the output collection will only contain the columns you have specified, so this may not be exactly what you're after.

------------------------------
John Carter
Professional Services
Blue Prism
Europe/London
------------------------------

Hi John,
This works well with VB but When I change the code to C# , I am getting compile error with the Split function. Can you please assist with the corresponding C# solution?

------------------------------
Kirtish Trivedi
------------------------------

Try this:
DataView v = new DataView(Collection_In); Collection_Out = v.ToTable(true, Column_Name_CSV.Split(','));​
------------------------------
Ami Barrett
Sr Product Consultant
Blue Prism
Plano, TX
------------------------------

Thanks for the code :)

------------------------------
Nilesh Jadhav
Senior RPA Specialist
ADP
India
------------------------------
Nilesh Jadhav.
Consultant
ADP,India