9

I know this very similar question has been asked before quite a few times such as in Comparing two collections for equality irrespective of the order of items in them, but I'm trying to use the solutions I'm reading there and something is not working... This HAS to be a stupid mistake I'm making... Please help!!

Ok, so, here's my scenario, suppose I have the following 3 lists created in code:

Dim lst1 as new list(of integer)
Dim lst2 as new list(of integer)
Dim lst3 as new list(of integer)

And then, within the code I end up with, say, the following values in the lists:

lst1:    lst2:    lst3:
1        1        2
2        2        3
3        3        4
4        4        5

So, obviously, lst1 & lst2 are equal and lst1 & lst3 are not, but what code can I write into my if statement to verify this?

I've tried:

lst1.SequenceEqual(lstXX)

And that returns True for both lst2 and lst3 I've tried:

lst1.Equals(lstXX)

And that returns False for both lst2 and lst3

Now, I know I could do it with code comparing count and lst1.Except(lstXX), but I'm wondering more so what I'm doing wrong here and even more importantly, what is the most efficient method to do this??

Thanks!!!

Community
  • 1
  • 1
John Bustos
  • 19,036
  • 17
  • 89
  • 151
  • 2
    Please show your code using `SequenceEqual` - it should be doing the right thing. I suspect you misdiagnosed it. – Jon Skeet Oct 25 '12 at 18:53
  • My Code is really that simple: - **If PathToStartDateForLinking(i).SequenceEqual(PathToStartDateForLinking(j)) Then** Where each of the two being examined are list(of integer) as specified in the question... – John Bustos Oct 25 '12 at 18:55
  • 1
    `lst1.SequenceEqual(lst3)` would never return true. Check your code. – L.B Oct 25 '12 at 18:55
  • More specifically, I have PathToStartDateForLinking as List(Of list(of Integer)) – John Bustos Oct 25 '12 at 18:56
  • Please show a short but complete program with the lists you've shown here, such that `lst1.SequenceEqual(lst3)` returns true. – Jon Skeet Oct 25 '12 at 18:57
  • What's really happening here is that `i` and `j` are not what you expect them to be. If you single-step through your code in a debugger you will see that the code is not doing what you think it's doing. – Gabe Oct 25 '12 at 19:09
  • Everyone, I apologize!!! - I was in the debugger and was doing everything right, but the code wasn't working and it frustrated the hell out of me, then I stopped the debugger and re-started the project and then it started working!!!! – John Bustos Oct 25 '12 at 19:11
  • I REALLY do apologize - this was a debugger quirk that I'll probably never understand and I'm VERY sorry for wasting your time!!! – John Bustos Oct 25 '12 at 19:12

2 Answers2

20

Assuming you want ordering to be important, SequenceEqual is exactly what you want - and in the sample you've given, lst1.SequenceEqual(lst3) will return false. Here's a short but complete program to demonstrate:

using System;
using System.Collections.Generic;
using System.Linq;

class Test
{
    static void Main(string[] args)
    {
        var lst1 = new List<int> { 1, 2, 3, 4 };
        var lst2 = new List<int> { 1, 2, 3, 4 };
        var lst3 = new List<int> { 2, 3, 4, 5 };

        Console.WriteLine(lst1.SequenceEqual(lst2)); // True
        Console.WriteLine(lst1.SequenceEqual(lst3)); // False
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1
class Program
{
    static void Main(string[] args)
    {
        var list1 = new List<int>() { 1, 2, 3, 4 };
        var list2 = new List<int>() { 1, 2, 3, 4 };
        var list3 = new List<int>() { 2, 3, 3, 4 };

        Console.WriteLine(list1.SequenceEqual(list2)); // True
        Console.WriteLine(list1.SequenceEqual(list3)); // False
        Console.ReadLine();
    }
}

Works as expected. Must be something else.

Are you certain that your i and j are what you think they are?

Are you certain that your lists are ordered as you've written them in your question? If they aren't ordered correctly, that SequenceEqual will return False.

itsmatt
  • 31,265
  • 10
  • 100
  • 164