I need to remove duplicates from a DataTable using LINQ. Specifically, I need to filter out all duplicates for a single column, maintaining the initial Datatable strucutre. Other methods, such as DefaultView, do not work as they print out only one column (the one containing duplicates).
Any suggestion would be highly appreciated, code examples would be terrific.
Hey @ncompa @Lucas.Pimenta answer will also work he just posted a c# version of Lambda Expressions and workflow supported Vb version so to use that in studion you have to follow the way mentioned by @florin.stan
@Lucas.Pimenta answet will also work just some modification will be needed (VB version) so studion will take it
what uipath activity are you putting this in? i am trying to do the same thing getting rid of duplicates but am unsure where you put this code. i have used Assign with LINQ but it gives me compile errors with this snippet you have.
(From x In dtNew.AsEnumerable()
.GroupBy(Function(i) i.Field(Of String)(âEmployee IDâ))
.Select(Function(g) g.First))
.CopyToDataTable()
Error is : â)â expecteed but i dont see where. I added paranâs at the end and before From and after First
If i just use it the way you had it earlier without my additional paranâs it tells me âEnd of expression expectedâ
Awesome, that works, thank you! Additionally i need to add a second âProjectIDâ column to the Select and GroupBy. Iâm trying to add that in as well but cant seem to get it right. Iâm not sure if i need to add all the Function stuff again and with different letters (i.e. âeâ and âgâ like you used) What would the syntax be for adding a second field to that query?
If youâre trying to first remove all employee id duplicates and then remove all project id duplicates, @Mr_JDaveyâs solution is the one you want. If youâre looking to remove all duplicate pairs of employee id and project id, you can make use of anonymous types:
dataTable = dataTable.AsEnumerable()
.GroupBy(Function(i) New With {
Key.EmployeeID = i.Field(Of String)("Employee ID"),
Key.ProjectID = i.Field(Of String)("ProjectID")})
.Select(Function(g) g.First)
.CopyToDataTable()
that worked! Is it possible to specify returning only those fields in the select part?
I tried Select(Function(g(âEmployee IDâ), g(âProjectIDâ)) but that didnât work.
Thanks florin.stan - youâve been a great help!
If you can help with the syntax on a follow-up question that would be greatâŚ
Now I have 2 datatables with the same structure and I want to write a linq to return where there is a record in dtNew but not in dtPrior based on comparing the 2 fields Employee ID and WBS Level3. I have it working with just one field but canât seem to get it working with both fields. Here is what I have so far that isnât working:
(From x In dtNew.AsEnumerable() Where not (From y In dtPrior.AsEnumerable() select y(âEmployee IDâ)).Contains(x(âEmployee IDâ)), y(âWBS Level3â)).Contains(x(âWBS Level3â)) select x).CopyToDataTable()
I would want the result to again be Employee ID and WBS Level 3