Take the unique value from column

Hi ,

I have a excel with a column called file name … i need to take filter the rows with the unique filename and make a datatable with it .

Ex

No. status FileName
1 Activated xyz.xlsx
2 success xyz.xlsx
3 Activated abc.xlsx
4 success abc.xlsx

initally i dt should contain
No. status FileName
1 Activated xyz.xlsx
2 success xyz.xlsx

I have few more steps i need to do with this datatbale .

Please help .

Hi @tharani.natarajan

Please try this

(From row In dt.AsEnumerable()
                Group row By FileName = row.Field(Of String)("FileName") Into Group
                Select Group.First()).CopyToDataTable()

Hope this helps!!

@tharani.natarajan

Use this simple linq query


(
From row In dt
Group row By a = row("FileName").ToString.Trim Into grp = Group
Select grp.First
).CopyToDataTable
1 Like

@tharani.natarajan

try this :

uniqueFileNameDT = originalDT.AsEnumerable().GroupBy(Function(row) row("FileName")).Select(Function(group) group.First()).CopyToDataTable()
  1. It groups the original DataTable (originalDT) by the “FileName” column to identify unique filenames.
  2. It selects the first row from each group, effectively keeping one row for each unique filename.
  3. It copies the selected rows into a new DataTable named uniqueFileNameDT.

You can then perform additional steps with the uniqueFileNameDT as needed.

cheerss…!

1 Like

Hi @tharani.natarajan

Use This Linq query:

Assign Activity:

uniqueFileNames = (From row In originalDT.AsEnumerable()
                   Select row.Field(Of String)("FileName")).Distinct().ToList()

Assign Activity:

filteredDT = (From row In originalDT.AsEnumerable()
              Where uniqueFileNames.Contains(row.Field(Of String)("FileName"))
              Select row).CopyToDataTable()

Hi @tharani.natarajan

Please try the below solution.
Dt1.DefaultView.ToTable(true, “Mobile”)

Dt1 is Datatable.
“Mobile” is the column name.

Change the code according to that.

@Divyashreem

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