From the course: Visual Studio Developer Tips

Compare LiNQ sequences

- [Instructor] This tip looks at a Linq method that is useful for comparing the contents of selected IEnumerables. For this example, the code is C#, and the IEnumerable type is the list of t. You can see that on line 14 through 17, where I'm declaring some lists of t. The goal, is to look at the contents of two lists and see if they contain the same sequence of items. So lets start by looking at the values that are placed in these lists. This first list, called numbers, contains 12 integer values. On line 15, I have the evenNumbers list, which contains even numbers from two through eight. Line 16, contains odd numbers one through nine, and line 17, the list named userSelectedNumbers, contains even numbers two through eight, so it has exactly the same integer values as the evenNumbers list. One way we can compare those is shown on line 21, where I used the C# == operator. You probably know what will happen. This will do a reference comparison, and since these are two separate instances of the list of int, that'll be considered not equal. Lets verify that, evenNumbers == userSelectedNumbers, is considered not equal. I'll come down to line 24 and use Ctrl + K, Ctrl + U, to uncomment those two lines of code. The key to comparing the sequences is this method called SequenceEqual. It's easy to use. You have the instance of one of the lists and you call .SequenceEqual, of course you'll need to have your Linq using statements in your code, and then you pass in the second list that you want to compare. Now we see that those two are considered equal; the sequence values are equal. They have four integer values in each, that's one way that the comparison works. The other is, what order are the integers contained in the list. So if I change these around, and swap four and two, and run my comparison, you'll see that these are no longer considered equal. Ctrl + Z to undo my changes, uncomment line 27 and 28. This is comparing the evenNumber list with the oddNumbers list, and I think you can guess what'll happen with that example. They're not equal. Now I've taught this, and shown this in classes, and one of the questions I get is, okay, what if I have two lists, or two IEnumerables, that I know are different, but I want to see what the differences are between the lists. Well here's one way you can accomplish that. Use the Except method, which says it produces the set differences of two sequences. So here I'm saying take numbers and then, the oddNumbers, and find the differences, store that in this variable, and then, here I'm using the ForEach method to output the results. There they are. The SequenceEqual method works for simple data types, also called primitive data types, this works as expected. If you build your own classes and lists of those classes, then it might work like you expect. So for this example, I will use a custom class that I have here, called Tour, that has three properties, TourName, AvailableDays, and AvailableMonths. Over here on line 37, I am instantiating an instance of the Tour class and setting the TourName to "Bikes in Barcelona", and the AvailableDays starts on Monday, and the AvailableMonths is April. Next, I declare another instance of the Tour class with exactly the same three property values, and then on line 51, I declare another Tour instance, but I use different values. Then I put those in lists. So for this example, I have a list of Tour, and I put bikeTour, and then sailingTour, and then for the second Tour list, I use the dupTour, and the sailingTour. So what's going to happen in this case is the SequenceEqual compares the references of the objects for complex data types. So since these two are not the same instance, they'll be considered to be not the same sequence, and it looks like I didn't uncomment the code that prints out the results, so lets do that, and try it again. So points to remember, the SequenceEqual method compares the number of items and their values for primitive data types. That's what we saw when we used the list of int. The SequenceEqual method compares the references of objects for complex data types, and if you want to have the SequenceEqual do property comparisons, so that in my case, the dupTour and the bikeTour are considered to be the same, based on the property values in it, they'll have to do some extra work. There's a couple ways you can accomplish this. You can modify your Tour class, or you could create an instance of a type that implements the iEqualityComparer class, and use that to compare the two collections, and then the SequenceEqual method will work as expected.

Contents