I have MyObject with field: id, a, b, c, e, f and I have List with 500 000 items, now how can I remove all duplicate items with of the same value of the parameter a, c, f? <
If what you are looking for is speed, and don't mind using up some memory then I would recommend that you use a HashSet
, if you are interested in doing some custom comparison, then you can make an IEqualityComparer
, something like this:
var original = new ArrayList(); // whatever your original collection is
var unique = new HasSet(new MyCustomEqualityComparer());
foreach(var item in original)
{
if(!unique.Contains(item))
unique.Add(item);
}
return unique;
the issue here is that you may end up gobbling up twice the original memory.
I made some extra research and I think you can achieve just what you want by simply doing:
var original // your original data
var unique = new HashSet(origin, new CustomEqualityComparer());
that should take care of removing duplicated data as no duplication is allowed in a HashSet
. I'd recommend that you also take a look at this question about GetHasCode
implementation guidelines.
If you want to know some more about the HashSet
class follow these links:
About HashSet
About IEqualityComparer constructor
IEqualityComparer documentation
hope this helps