Identify Unique rows based on specific columns

Hi I want to find unique rows from a datatable but want to apply the unique identification criteria on only few columns but the result should return the other columns also.
for eg
column1, column2,column3,column4
ABC,xxx,123,yyy
ABC,xxx,123,yyy
tys,yyy,343,yyy
tys,yyy,343,yyy
yru,ooo,999,ppp

I want to apply the unique filter criteria only on columns 2,3,4 . How can achieve this in C# in uipath?

Output Should be

column1, column2,column3,column4
ABC,xxx,123,yyy
tys,yyy,343,yyy
yru,ooo,999,ppp

Hi @vishal_nachankar

we can achieve this using LINQ

kindly try below expression in assign activity

dt.DefaultView.ToTable(True,"ColumnName")

This will return a new datatable having only one column and distinct values of that column from the original table

You can then iterate over this new datatable and in each iterationn use the datatable.select method for accessing only those rows where the distinct value
matches.

or another method


dt = dt.AsEnumerable.GroupBy(Function(row) row(“ColumnName”)).Select(Function(row) row.First).CopyToDataTable

Thanks

it dont want only one column i want to apply unique filter criteria on the columns mentioned and a result should be the whole original datatable columns

Hi @vishal_nachankar

Try this

dt = dt.AsEnumerable.GroupBy(Function(row) row(“ColumnName”)).Select(Function(row) row.First).CopyToDataTable

Can u share the code in C# the linq if possible

can anyone please help me out

it is working on single column i need to apply it on multiple columns and the syntax in C#

Hi @vishal_nachankar

for multiple columns
try this

Dt.AsEnumerable().GroupBy(Function(g) Tuple.Create(g(1).ToString,g(2).ToString,g(3).ToString)).Select(Function(s) dt.Clone.LoadDataRow(s.First().ItemArray.ToArray,False)).CopyToDataTable()

i am not sure about LINQ syntax in C# so far , any specific reason for C#

my framework is in C# so

If any one aware of group by syntax for multiple columns in C# for above case please share it

Hi @vishal_nachankar

I am tagging yoichi the linq query king :slight_smile: @Yoichi

Thanks

1 Like

Hi,

How about the following expression?

dtResult = dt.AsEnumerable().GroupBy(r=>Tuple.Create(r["column2"].ToString(),r["column3"].ToString(),r["column4"].ToString())).SelectMany(g=>g.Distinct(DataRowComparer.Default)).CopyToDataTable()

Sample20220404-6CS.zip (2.7 KB)

Regards,

Hi @vishal_nachankar ,

I’d like to confirm your requirement.

If input data is as the following,

column1,column2,column3,column4
ABC,xxx,123,yyy
ABC,xxx,123,yyy
AAA,xxx,123,yyy
tys,yyy,334,yyy
tys,yyy,334,yyy
yru,ooo,999,ppp

Is expected output still the following?

column1, column2,column3,column4
ABC,xxx,123,yyy
tys,yyy,343,yyy
yru,ooo,999,ppp

OR

column1,column2,column3,column4
ABC,xxx,123,yyy
AAA,xxx,123,yyy
tys,yyy,334,yyy
yru,ooo,999,ppp

If former, the expression will be as the following.

dtResult = dt.AsEnumerable().GroupBy(r=>Tuple.Create(r["column2"].ToString(),r["column3"].ToString(),r["column4"].ToString())).Select(g=>g.First()).CopyToDataTable()

Regards,

1 Like

Thanks @Yoichi my requirement was for the second query which u shared.

1 Like

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