This is probably a general question, and I was looking for another question without finding a solution that works (note: my skill in C # and linq is limited - so a simple solution will be appreciated!).
Here is the problem :
I have 2 lists with objects. I want to compare them and return all new objects in list2.
Example:
List ObjectList List1; // contains 3 objects that are stored in the database
List ObjectList List2; // contains the same 3 objects as in List1, and a new object added from the web page (the parent object was updated on the web page).
List ObjectList List3; // should compare List1 and List2 and return NEW objects in List2 (so the result should be only Object number 4)
Note:
- Order doesn't matter. I want only new object (s)
- Usually objects are added only to List2. IF any object is deleted (compare with List1), then this should be ignored. (therefore, an object that exists only in List1 is not of interest)
Thanks for any suggestions or links to previous questions that I missed in my search
Edit
Here is a small example of the first attempt with Except (this returned an error)
I cut it a bit. The method is from our software, so they probably do not know you. Sorry.
Solution that worked The exception did not work, because the list is just a reference (not a value). You can use the second parameter, but I got linq code that worked:
RepositoryObjectList caNewCA = new RepositoryObjectList(caDialogObjects.Where(item1 => !caObjectObjects.Any(item2 => item1.Id == item2.Id)));
source share