C # Compare two lists, return new items to list 2

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.

// 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, 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))); 
+6
source share
4 answers

Use this:

 var list3 = list2.Except(list1); 

This uses the Except extension method, which returns all the 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 list3 for 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)); 
+14
source
  List<MyObject> objectList1 = new List<MyObject>(); List<MyObject> objectList2 = new List<MyObject>(); List<MyObject> objectList3 = objectList2.Where(o => !objectList1.Contains(o)).ToList(); 

This should do it while MyObject is IComparable

http://msdn.microsoft.com/en-us/library/system.icomparable.aspx

+4
source

Here is an example of how you could do this with LINQ:

 List<int> l1 = new List<int>(); List<int> l2 = new List<int>(); l1.AddRange(new int[] {1, 2, 3, 5}); l2.AddRange(new int[] {1, 2, 3, 4}); // get only the objects that are in l2, but not l1 var l3 = l2.Except(l1); 

The third list will contain only one item, 4.

+2
source
 secondList.Except(firstList) 

which uses

 public static IEnumerable<TSource> Except<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second ) 

http://msdn.microsoft.com/en-us/library/bb300779.aspx

or

 secondList.Except(firstList,new CustomComparer()) 

which uses

 public static IEnumerable<TSource> Except<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer ) 

http://msdn.microsoft.com/en-us/library/bb336390.aspx

+1
source

Source: https://habr.com/ru/post/887118/


All Articles