I’m sorry if this has been covered already, but I haven’t been able to find a solution. I need to remove rows from a DataTable based on a few duplicate columns.
For example, my input could be something like:
and I need to filter this to return:
The problem I’m having is that because of the ID column, none of the rows are completely unique. I need to filter to get unique rows based on “col1” and “col2” but still return all 3 columns for each “unique” row. In other words, I only want to consider col1 and col2 when determining which rows are duplicates.
I’ve tried both Excel Remove Duplicates Range and Data Table Remove Duplicate Rows but wasn’t able to get the required results. I would imagine it could be done with Linq, but I’m not too familiar with it.
I didn’t get any errors. I just couldn’t get it to work. Remove Duplicate Rows would only remove rows that were exactly the same. Remove Duplicates Range would remove duplicate cells in the range I selected but leave the other columns/rows in tact. It seems to be more of a “Remove Duplicate Cells”.
Yes. Remove row ID3 because col1 and col2 are the same as ID2 and similar with ID7.
I was hoping to avoid avoid sorting and running through a “for each” loop. There should be an easier way to do it. I’m pretty sure Linq can do it but I don’t know it well enough.
Thanks, but I already tried that. It only returns col1 and col2 but not the ID column. In essence I wanted to return all the columns but only use col1 and col2 when determining the “duplicate” rows.
Thank you everyone for your suggestions. After giving it some more thought, I’ve determined that I don’t need the “ID” column anymore so filtering based only on col1 and col2 is simple.
However, if anyone has a simple solution to my original query, I’d love to see it for the experience.
I beg to differ. I just tried it and it only returns col1 and co2. the ID column has been removed. Taking a look at my actual work (not the sample), I have 17 columns in my dataTable when I read the full range. After using your example with the DefaultView, my output dataTable only has 2 rows.