I have two List(Of String)
named MethodCodes
and MethodDescriptions
. I would like to create unique values.
E.g. I have these values in the lists...
MethodCodes (45, 45, 46a, 46b, 47, 47)
MethodDescriptions (meth45, meth45, meth46, meth46, meth47, meth47)
I need to reduce this Lists to this...
MethodCodes (45, 46a, 46b, 47)
MethodDescriptions (meth45, meth46, meth46, meth47)
Actually, the unique must be values in MethodCodes list, but the count of items must be the same in both lists.
Tomas
If the values in these two list have an association then it would be a much better idea to keep them associated in an appropriate data structure such as a class.
A simple class like this would do it:
Private Class Method
Public Property Code As String
Public Property Description As String
Public Sub New(code As String, description As String)
Me.Code = code
Me.Description = description
End Sub
End Class
This allows you to add values like this:
Dim methods As New List(Of Method)
methods.Add(New Method("45", "meth45"))
methods.Add(New Method("45", "meth45"))
methods.Add(New Method("46a", "meth46"))
methods.Add(New Method("46b", "meth46"))
methods.Add(New Method("47", "meth47"))
methods.Add(New Method("47", "meth47"))
And then find the distinct Code values and the associated descriptions like this:
For Each distinctMethod In methods.Distinct.GroupBy(Function(x) x.Code).Select(Function(d) d.First()).ToList()
Debug.WriteLine(distinctMethod.Code & "-" & distinctMethod.Description)
Next
Output:
45-meth45
46a-meth46
46b-meth46
47-meth47