This is probable a common question, and I have searched other question without finding a solution that works (note, my skill in C# and linq is limited - so a simple solution would be appreciated!).
Here is the issue:
I have 2 lists with objects. I want to compare them and return all the NEW objects in list2.
Example:
ObjectList List1; // contains 3 objects that is stored in the database
ObjectList List2; // contains the same 3 objects as in List1 and a new object that was added from a webpage (the parent object was updated on the webpage)
ObjectList List3; // should do a compare of List1 and List2, and return the NEW objects in List2 (so the result should only be Object number 4)
Note:
- The order does not matter. I only want the new object(s)
- Normally objects are only added to List2. IF any object is removed (compare to List1), then this should be ignored. (so object that only exists in List1 is of no interest)
Thanks for any suggestions or links to previously questions i missed in my search
Edit
Here is a small example of first try with Except (this returned an error)
I have shortened it a bit. The method is from our software, so they are probable not know to you. Sorry about that.
// caDialogObjects = List1 (caDialogQLMLinks is the link to the objects)
RepositoryObjectList caDialogObjects = args.Object.GetConfiguration().GetObjectSet(caDialogQLMLinks);
// caObjectObjects = List2 (caObjectQLMLinks is the link to the objects)
RepositoryObjectList caObjectObjects = args.Object.GetConfiguration().GetObjectSet(caObjectQLMLinks);
// List 3
RepositoryObjectList caTotal;
caTotal = caObjectObjects.Except(caDialogObjects);
Solution that worked The Exception did not work since the list is just a reference (not a value). It is possible to use the second parameter, but i got a linq code that worked:
RepositoryObjectList caNewCA =
new RepositoryObjectList(caDialogObjects.Where(item1 =>
!caObjectObjects.Any(item2 => item1.Id == item2.Id)));