0

I have a C# application. I have two lists ('NewStock' and 'OldStock') of the same custom type, 'Book'. I want to extract any 'Book' object that is the list NewStock but not in the OldStock list into a new list called ResultList. Please see an example below. How do I do this?

I have used implemented the IEqualityComparer interface on my class Book so that I check if a book object is equal to another.

 NewStock      OldStock
 A             A
 B             C
 C            
 D

Result I would like,

ResultList
B
D
saggu
  • 73
  • 5
mHelpMe
  • 6,336
  • 24
  • 75
  • 150

3 Answers3

4
var results = newStock.Except(oldStock).ToList() 

And you can provide a custom comparer to Except to do this.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
2

Use except to do this:

Example can be found here: MSDN

Henrik
  • 1,797
  • 4
  • 22
  • 46
1

Try this one:

List<Book> ResultList = NewStock.Select(x=>!OldStock.Contains(x)).ToList();
Christos
  • 53,228
  • 8
  • 76
  • 108