How to remove duplicate object in List with double key

You can group with multiple fields using the following syntax, grouping will filter out the duplicate entries:

var testListNoDups = testList.GroupBy(x => new {x.itemId, x.itemTypeId})
                                  .Select(x => x.First())
                                  .ToList();

if you want to modify existing testList, you can try RemoveAll:

 HashSet<Tuple<int, int>> keys = new HashSet<Tuple<int, int>>();

 testList.RemoveAll(x => !keys.Add(Tuple.Create(x.itemId, x.itemTypeId)));     

Here we try to add next key to keys and in case of failure (i.e. key exists in keys) we remove the item from testList

Tags:

C#