2

I have list of Fruit:

List<Fruit>

where Fruit has the following properties/fields:

id
name
color

Given an array of integers:

int[] ids = new [] {1,2,8};

How can I filter my list so that it will exclude the fruits whose id is in the array?

2
  • 1
    What have you tried so far? You'll get more responses here if you can show some code (even if it's not working). Commented Apr 4, 2016 at 18:36
  • Possible duplicate of Using LINQ to remove elements from a List<T> Commented Apr 4, 2016 at 19:12

1 Answer 1

4
var l = new List<Fruit>();
var exceptions = new int[] {1,2,8};
var filtered = l.Where(x=> !exceptions.Contains(x.id));

Note that this will return a new filtered IEnumerable<Fruit>; it will not remove the items from the original list. To actually remove them from the list, you can use instead:

l.RemoveAll(x => exceptions.Contains(x.id));
Sign up to request clarification or add additional context in comments.

Comments

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.