20

I have 2 lists of different objects (foo & bar) that share the same property lets call it id.

public List<foo> foo { get; set; }
public List<bar> bar { get; set; }

I want to remove all objects from foo that have an id that does not exist in bar

How can this be done in linq? I have been looking at Intersect, RemoveAll & Join but cannot find any example where the lists are of a different type.

2 Answers 2

31

Try this:

foo.RemoveAll(x=> !bar.Any(y=>y.Id==x.Id));

!bar.Any(y=>y.Id==x.Id) will get if item is in bar collection and if it's not it will remove it from foo collection.

Better solution using hashset O(n):

var idsNotToBeRemoved = new HashSet<int>(bar.Select(item => item.Id));                     
foo.RemoveAll(item => !idsNotToBeRemoved.Contains(item.Id));

source of second answer: https://stackoverflow.com/a/4037674/1714342

EDIT:

as @Carra said, first solution is good for small lists and second is more efficient for big lists.

Sign up to request clarification or add additional context in comments.

1 Comment

For small lists, the first will do. If you're using big lists (> 100 or so), you're better of using the second solution.
8
var foo = foo.Where(f => !bar.Any(b => b.Id == f.Id)).ToList();

Just keep in mind that this is a O(n²) solution, it won't work very well for big lists.

5 Comments

What would be a more effcient solution to this? @Carra
@ojhawkins One solution would be to pre-collect the list of ids from bar into a hashset (O(n)) then compare each element in foo to that (O(n)) rather than walking the bar list for every element in foo
yes I did see Jon Skeets example here but could no apply it to my example stackoverflow.com/questions/853526/…
new HashSet<int>(bar.Select(x => x.id)) ought to do, assuming you have no collisions
Using a hashset to save your ids should work, check wudziks example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.