I have having trouble figure out how to write this code.
I have a list of items, the items all have an ID. There is also another value that we will call otherID. If
Like this:
list.RemoveAll(r => list.Any(o => o != r && r.ID == o.otherID));
One way to do this would be a 2-stage process:
This will require two passes of the list. Since adding to a HashSet / testing if it contains an item should be a constant-time operation, the entire operation should run in linear time.
var idsToBeRemoved = new HashSet<int?>(list1.Select(item => item.otherID)
.Where(otherId => otherId.HasValue));
list1.RemoveAll(item => idsToBeRemoved.Contains(item.ID));
EDIT: Updated Id
's type to int?
after clarification from the OP.