From the course: Visual Studio Developer Tips
Compare LiNQ sequences - Visual Studio Tutorial
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
-
-
-
Brace matching in C#4m 57s
-
Select vertical sections of code with Box Selection5m 50s
-
Move code with keyboard shortcuts2m 49s
-
Find duplicate code with the code clone analysis tool4m 8s
-
Paste JSON/XML as class3m 42s
-
Colorize output in the build window4m 37s
-
A better way to redock a tool window3m 57s
-
The Open Command Line extension3m 39s
-
Get consistent editor settings with EditorConfig3m 46s
-
Git at a glance: Updates to the Visual Studio status bar4m 1s
-
Surround a block of code with a code snippet2m 5s
-
Understand the document tab features3m 35s
-
Clipping to a border using an opacity mask5m 53s
-
Insert Line with Ctrl Enter2m 21s
-
Use the File Tab channel3m 31s
-
Use the Add New File extension4m 27s
-
Keyboard access for CodeLens4m 32s
-
Use the Clipboard Ring2m 56s
-
Use the GlyphFriend extension5m 41s
-
Activate Open File in Solution Explorer3m 14s
-
Use the enhanced scrollbar41s
-
Drag and drop in Solution Explorer5m 20s
-
Keyboard shortcuts for MVC views and controllers4m 17s
-
Pin data tips in debug window6m 6s
-
The Trailing Whitespace Visualizer extension4m 19s
-
Date rounding and other helpers5m 21s
-
Memorize window layout (Reuse)4m 26s
-
Use DialogResult6m 48s
-
Label a breakpoint2m 31s
-
The Image Optimizer extension2m 35s
-
Use Ctrl+Spacebar to fix case of variable names3m 40s
-
Use the nameof operator6m 21s
-
Discover untested code with code coverage7m 36s
-
Use the Hand tool in XAML designer2m 16s
-
Array Visualizer extension2m 35s
-
Use Ctrl+Tab to cycle through windows3m 18s
-
Understand line stroke patterns4m 37s
-
The Comment Remover extension3m 41s
-
Toolbox tip shortcuts6m 1s
-
Randomize data with LINQ4m 13s
-
Customize context menu4m
-
Get the file path1m 25s
-
Convert lists4m 34s
-
Enum flags5m 19s
-
Reference highlighting2m 4s
-
Evaluate performance with PerfTips4m 50s
-
Null propagation4m
-
Multiple editor views of same file2m 17s
-
Customize the output window7m 37s
-
Colored braces with the Viasfora extension4m 15s
-
Use the C# interactive window10m 47s
-
The Smart Semicolon extension3m 29s
-
Reference Highlighting1m 35s
-
IEnumerable Extension Methods4m 58s
-
Compare LiNQ sequences6m
-
The Clear Recent Project List extension3m 34s
-