Reputation: 2206
My code
query1 = select value1, value2 from dbo.animal where animalname="tiger";
query2 = select value1, value2 from dbo.animal where animalname="lion";
List<Animal> list1 = db.ExecuteStoreQuery<Animal>(query1).ToList();
List<Animal> list2 = db.ExecuteStoreQuery<Animal>(query2).ToList();
// list1.Count = 1 list1[0].value1 = "a" list1[0].value2 = "b"
// list2.Count = 1 list2[0].value1 = "a" list2[0].value2 = "b"
// Now I would like to compare list1 and list2 to see if they are equal
var list3 = list1.Except(list2);
//At this point list3 is containing both list1 and list2 and using var made it
//GenericList not the same type of list as List1 and List2
I tried List list3 = list1.Except(list2) but I get compile error.
So the question is how do I find out if list1 is equal to list2? I was hoping list3 would be the differences between list1 and list2 and therefore if the lists are equal list3.count() should be 0.
The nice thing about my data is I believe data in from query1 and query should be both in order and result in only 1 record each.
Upvotes: 1
Views: 1678
Reputation: 726579
First of all, checking if the results of Except
are empty cannot answer the "are these two lists equal?" question. It answers the "does list2
contain all the elements of list1
?", which is not the same, because list2
may contain additional elements.
The nice thing about my data is I believe data in from query1 and query should be both in order and result in only 1 record each.
You can compare two identically ordered lists for equality using SequenceEqual
, like this:
bool areIdentical = list1.SequenceEqual(list2);
If the ordering is different, you can also force it to be the same with OrderBy
:
bool areIdentical = list1
.OrderBy(animal=>animal.Id)
.SequenceEqual(list2.OrderBy(animal=>animal.Id));
Upvotes: 1