Need to remove duplicate Rows based Column Name


I am trying to remove the duplicate rows from datatable based on one of the column Name.

Using below Code

masterFinalDT.AsEnumerable().GroupBy(Function(a) a.Field(Of String)(“PO Number”)).Select(Function(b) b.First).CopyToDataTable()

I got the Error : Assign: Unable to cast object of type ‘System.Double’ to type ‘System.String’.

So i tweaked like this,
masterFinalDT.AsEnumerable().GroupBy(Function(a) CType( a.Field(Of String)(“PO Number”),string)).Select(Function(b) b.First).CopyToDataTable()

But still getting same error any leads helpful

give a try on

(From d in masterFinalDT.AsEnumerable
Group d by k=d("PO Number").toString.Trim into grp=Group
Select r = grp.First()).CopyToDataTable

that issue can come from the Field function. When it is assumed that the column values are of Double an Foeld(Of String) will fail, as it the method doesnt have a cast/conversion functionality built in.

What is “d” here ? is it just assumption keyword?

d is acting like a reference / local variable within the LINQ which is formulated within Query Syntax.

Similar to Function (a) a.Field

Thank you it worked

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