Search code examples
vb.netuniquedistinct

VB.NET Unique String Combinations Problem


I have two classes. 1 houses all of my records and one I want to only house my unique string combinations.

Public Class Records
    Public Property ID As String
    Public Property TNumber As String
    Public Property TUnit As String
    Public Property SiteName As String
    Public Property FName As String
    Public Property LName As String
    Public Property MName As String
    Public Property Suffix As String
    Public Property Address1 As String
    Public Property Address2 As String
    Public Property City As String
    Public Property State As String
    Public Property PostalCode As String
    Public Property Email As String
    Public Property HomePhone As String
    Public Property MobilePhone As String
    Public Property Gender As String
    Public Property HireDate As String
    Public Property BirthDate As String
End Class
Private Class StringsUnique
    Public Property S1 As String
    Public Property S2 As String

    Public Sub New(s1 As String, s2 As String)
        Me.S1 = s1
        Me.S2 = s2
    End Sub
End Class

I want to get unique string combinations of TNumber And TUnit and place it in StringsUnique.
Combinations Example:

"02547", "03"
"02547", "05"
"02547", "03"

How can I go through the class so that I only get unique results "02547, 03" and "02547, 05"?

Here is a loop example that I have tried but returns all string combinations...

Dim InsertCheck As Boolean = False For I = 0 To Records.Count - 1

            If StringsUnique.Count <> 0 Then

                For U = 0 To StringsUnique.Count - 1

                    If (StringsUnique.Item(U).S1 <> Records(I).TNumber.ToString And StringsUnique.Item(U).S2 <> Records(I).TUnit) Or (StringsUnique.Item(U).S1 = Records(I).TNumber.ToString And StringsUnique.Item(U).S2 <> Records(I).TUnit) Or (StringsUnique.Item(U).S1 <> Records(I).TNumber.ToString And StringsUnique.Item(U).S2 = Records(I).TUnit) Then

                        InsertCheck = True

                    End If

                Next

            Else

                StringsUnique.Add(New StringsUnique(Records(I).TNumber.ToString, Records(I).TUnit.ToString))

            End If

            If InsertCheck = True Then

                StringsUnique.Add(New StringsUnique(Records(I).TNumber.ToString, Recprds(I).TUnit.ToString))

            End If

            InsertCheck = False

        Next

Solution

  • Dim q = (From r In recs Select New With {Key .S1 = r.TNumber, Key .S2 = r.TUnit} Distinct).ToList()

    Q is now a list of distinct string combinations!

    Thanks – GSerg