0

There are 2 collections. Coll A is items = {1,2,3,4,5}. Coll B = { 3, 6, 7 } Need to find the common item and then remove all the other items from Coll A. So the result is this example should be Coll A = { 3 }.

Can you a please let me know a Simple and elegant way..

Thanks

Andy
  • 33
  • 3

2 Answers2

4

You can you use the LINQ Intersect method:

collectionA = collectionA.Intersect(collectionB).ToList();
dlev
  • 48,024
  • 5
  • 125
  • 132
  • Another instance is where I have a object and there is list of ID's that get passed. How do I remove the rows corresponding to these Id's – Andy May 23 '11 at 04:22
0

Try this: http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#intersect1. LINQ provides a method called Intersect which operates the same as a mathematical intersection of two sets should.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • Another instance is where I have a object and there is list of ID's that get passed. How do I remove the rows corresponding to these Id's – Andy May 23 '11 at 03:54
  • @Andy You really should be posting a new question. However, I think this should help you [http://stackoverflow.com/questions/853526/c-using-linq-to-remove-objects-within-a-listt](http://stackoverflow.com/questions/853526/c-using-linq-to-remove-objects-within-a-listt) – Tieson T. May 26 '11 at 03:53