Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Compare two lists, return the new items in list 2

Tags:

c#

list

compare

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)));
like image 377
Kim Avatar asked May 02 '11 12:05

Kim


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Is C++ same as JavaScript?

JavaScript is a scripting whereas C++ is a programming language. C++ program is to be compiled and executed, whereas a script in JavaScript is interpreted. JavaScript is dynamically typed whereas C++ is statically typed.


1 Answers

Use this:

var list3 = list2.Except(list1);

This uses the Except extension method which returns all elements in list2 that are not in list1.
It is important to note, that Except returns an IEnumerable<T> where T is the type of the object inside list1 and list2.
If you need your list3 to be of a specific type, you need to convert that return value. In the simplest case, your target type has a constructor that can handle and IEnumerable<T>:

RepositoryObjectList list3 = new RepositoryObjectList(list2.Except(list1));
like image 191
Daniel Hilgarth Avatar answered Sep 28 '22 12:09

Daniel Hilgarth