Comparing custom objects c#


I have the following situation:

I have a loop which could loop any amount of times. Within that loop I am calling a method which returns a List<CustomClass> After I have run through the loop I need to be able to compare all the List<CustomClass> items from each list and see which ones are common between all of them. In order to do this I have tried to put all the List<CustomClass> into another list: List<List<CustomClass> and then I need to use all of these to see which ones exist in all of them. I will be comparing on one property of my CustomClass (string Name)

This is what I have so far

public class CustomClass { public string name; } public List<CustomClass> SomeMethod() { List<List<CustomClass>> bigList = new List<List<CustomClass>>(); List<CustomClass> finalList = new List<CustomClass>(); for (int i=0;i<=5;i++) { List<List<CustomClass>> newList = GetNewList(); bigList.Add(newList); } //I now need to compare everything in bigList and create a new list with all common //items in the list of bigList. return finalList ; } public List<CustomClass> GetNewList() { List<CustomClass> newList = new List<CustomClass>(); for (int i=0;i<=5;i++) { CustomClass newClass = new CustomClass(); newClass.name = "some name"; newList.Add(newClass); } return newList; {

I hope this makes sense. Any help on this is much appreciated.



For example in List<List<CustomClass>> each List<CustomClass> contains a CustomClass with name set to "Pete", I then want to create a CustomClass with name set to "Pete" and add it to the final list.



see which ones are common between all of them


Use the Intersect extension method:

var common = list1.Intersect(list2);

Note that for this to work, you should either:

<ul><li>override Equals and GetHashCode in CustomClass</li> <li>make CustomClass implement IEquatable<CustomClass></li> <li>provide Intersect with a custom comparer that implements IEqualityComparer<CustomClass></li> </ul>


  • How to get the second element from the sublist using python
  • The collection has not been initialized
  • JavaScript reduce can't handle Math functions?
  • Different slicing behaviors on left/right hand side of assignment operator
  • AbstractTableModel contains proper data but will not update on fireTablDataChanged
  • How to do lappend in a while loop using regexp
  • Copying Excel data to Text File - Java (POI)
  • Merge Multiple Lists of Lists Based on Template
  • Delphi XE3, ugly StringGrid's borders
  • Compare variables PHP
  • SharePoint Designer 2010 - Determine if today's date is within x days of a start date column us
  • rails - convert DateTime to UTC before saving to server
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Xcode 4 NSLog Macro link in Xcode 3
  • Database structure design with variable amounts of fields
  • Breeze - Deleted Items nav properties bug
  • Java: can you cast Class into a specific interface?
  • How do I fake an specific browser client when using Java's Net library?
  • javaw.exe and eclipse startup problems
  • How to get a value (ex: baseURL) in every Karate feature?
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • need help with bizarre java.net.HttpURLConnection behavior
  • File not found error Google Drive API
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal