Need help with operators for list of string
Hello, I'm trying to compare two arrays (list of strings) however I'm not sure if I'm doing it right. My code
Code:
Dim oldArr As New List(Of String)
oldArr = GetOldValues(productNameStr)
If oldArr.ToString() IsNot newArr.ToString() Then
...
I want to know if the last statement is right otherwise how to correct it.
Re: Need help with operators for list of string
Assuming two equal length List(Of String) a1 and a2;
Code:
Dim TheSame As Boolean = a1.Zip(a2, Function(x1, x2) x1 = x2).All(Function(a) a = True)
or
Code:
Dim TheSame As Boolean = a1.SequenceEqual(a2)
or
Code:
Dim Same As Boolean = True
For Each item1 As String In a1
If Not a2.Contains(item1) Then
Same = False
MessageBox.Show("a2 does not contain " & item1)
End If
Next
Re: Need help with operators for list of string
hello, thank you for your reply. i'm currently trying the code you gave and it will take some time for me to get the result. would this code also work with dictionaries?
Code:
Dim TheSame As Boolean = a1.Zip(a2, Function(x1, x2) x1 = x2).All(Function(a) a = True)
Re: Need help with operators for list of string
No, not exactly. I'd also recommend using one of the others, as I find them to be more clear, and they will likely have somewhat better performance.
An item in a List(of String) is a String. An item in a dictionary, regardless of type, is a key,value pair. Most likely, the comparison you'd want would be different from comparing key,value pairs. You might compare the keys, or both the keys and the values that go with the keys (do the two have the same set of keys, and if so, do they have the same values for the same keys).
Also note that, if there is any chance that the two collections might be different lengths, comparing the lengths as a first step is a quick and easy thing to check first.