Ponco Herbowo Ponco Herbowo - 1 month ago 12
Vb.net Question

Filter unique distinct values using first 3 digit vb.net

I need to filter the phone number by the cell operator in vb.net
example:


  • operator 1 first 3 digit 811

  • operator 2 first 3 digit 812

  • operator 3 first 3 digit 822



from this


  • 8113634563

  • 8122345235

  • 8226786767



to this


  • operator 1 first 3 digit 811 8113634563

  • operator 2 first 3 digit 812 8122345235

  • operator 3 first 3 digit 822 8226786767


Answer

Check this example:

    Sub Main()
        Dim phones As String() = {"8113634563", "8113777763", "8122345235", "8226786767"}

        Dim s As Dictionary(Of String, List(Of String)) = phones.GroupBy(Function(ph) ph.Substring(0, 3)).ToDictionary(Function(g) g.Key, Function(g) g.Select(Function(p) p).ToList)

        For Each item In s
            Console.WriteLine("{0}", item.Key)

            For Each p In item.Value
                Console.WriteLine("operator first 3 digit {0} {1}", item.Key, p)
            Next

            Console.WriteLine()
        Next

        Console.ReadLine()
    End Sub