Question

I am working in vb.net. I have two generic lists of a user defined class - lets call this class students.

I am issuing a call to a database which gives me a dataset which I arrange into a generic list of students. Student will have properties like: firstname, lastname, phone, address, etc.

I then issue a call to a foreign web api that I arrange the data back into another generic list of students - same class.

Now I am trying to compare one for one if there are any differences in the database side from what I get in the call to the web api. Is there an easy way to compare generic list of class like this to another object? I cannot be sure of the order of the items so that makes a direct comparison not possible - but is there an obvious way I am missing other than iterating through the generic list and searching for the match in the other generic list.

Lets call them... DatabaseList and APIList

There are these possibilities:

  1. Exists in Database but not API
  2. Exists in API but not Database
  3. Exists in both but not matching between the two

I would love to end up with three new lists so that I can take appropriate actions. Right now the only way I can pull this off is to iterate through the lists and that seems like a bad idea - there must be a better way.

Was it helpful?

Solution

There is an Enumarable.SequenceEqual method for comparing generic sequences. You would probably need to sort the collections first and you would need to implement IEqualityComparer(Of T) for the items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top