Choosing a data structure in VB.NET and performing comparison
I'm a Python programmer and VB.NET newbie converting an application from Python to VB.NET (3.5).
In Python, I have a function which returns a list of tuples which I run on two datasets with results like this:
data1 = [(1,"a",2),(5,"c",7)...]
data2 = [(1,"a",2),(5,"x",7)...]
Then I want to check if the two data sets are identical. In Python I check for equality like this:
"Equal" if data1 == data2 else "Not Equal"
I want to know the easiest way to structure the data in VB.NET.
It looks like the right data structure for each data set in VB.NET is a List(of Something).
Should I create a Class to hold each data ite开发者_Python百科m, or is there a simpler way? If I do, will I need a custom way to decide if two instances hold the same data?
What is the easiest way to compare the two data sets for equality?
You can use the Tuple(Of T1, T2, T3)
generic type, or make your own class.
Either way, you'll need to create an IEqualityComparer(Of T)
for the class; you can then check whether set1.SequenceEqual(set2, New MyComparer())
.
If you create your own class, you can override Equals()
and GetHashCode()
instead of creating an IEqualityComparer
.
Personally, I'd create a small class to hold each item, then use List(Of ItemType) to track the lists. As for comparing the two lists for equality, see here Comparing two collections for equality irrespective of the order of items in them
精彩评论