none
Is it possible to make SortedList(Of).IndexOfValue() parallel

    Question

  • Hi

    I have class named Toponyme with two properties  sName and sCode declared as string

    And a list of Toponyme declared with

    Dim Com as SortedList(Of string, Toponyme)

    THere is more than 30,000 items in Com

    I have some procedures to retrieve information like:

    Dim i As Integer = CommunesSource.IndexOfValue((From oCom As Toponyme In CommunesSource.Values
                                                                    Where oCom.sCodePostal = .sCodePostal _
                                                                         And oCom.sNom = .sNom).FirstOrDefault)
                    If i >= 0 Then
                        'On  a trouvé le toponyme par son Code Postal
                        oTo = CommunesSource.ElementAt(i).Value
                        Return True
                    Else
                        Return False
                    End If

    It works fine but as I have to make these queries on other properties and that there is a lot of items the process is vey long.

    Is there any possibility to make these in paralel?

    THanks

    Bernard


    Bernard Bouree

    Wednesday, May 11, 2016 10:50 AM

Answers

  • If it helped, please mark it as the solution and Vote on the posts.

    Merci

    • Marked as answer by BernardBouree Wednesday, May 11, 2016 4:45 PM
    Wednesday, May 11, 2016 4:06 PM
  • Thanks for having marked the post.... could you mark the one with code instead...it makes it easier for people searching solutions...

    What do you mean automate the increase? in the code proposed it increases by 1 after each successful read of a Commune data.

    'this is the first column, and the PrimaryKey for the table

    datatableToponymes.PrimaryKey = New DataColumn() {IDColumn}

    later the first column is loaded with the index:

    datatableToponymes.Rows.Add(New Object() {nbToponyme, oneCommune(1), oneCommune(2), oneCommune(0), oneCommune(3)})

    and the index is increased:

    nbToponyme += 1


    Modified signature....



    Wednesday, May 11, 2016 4:54 PM

All replies

  • Before introducing the parallelism, maybe you also consider a possible improvement like this:

    oTo = (From oCom As Toponyme In CommunesSource.Values
            Where oCom.sCodePostal = .sCodePostal _
              And oCom.sNom = .sNom).FirstOrDefault
    
    Return oTo IsNot Nothing

    Check if it works correctly and faster.

    • Edited by Viorel_MVP Wednesday, May 11, 2016 11:47 AM
    Wednesday, May 11, 2016 11:45 AM
  • DataTables are there for that.

    Here is a quick example using the Communes datatable from LaPoste 39200 records...:


    And the loading code is:

    Public datatableToponymes As New DataTable
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(200, 200)
            'Loading the Data
            Dim srFichierCommunes As StreamReader
            Dim oneLine As String = ""
            Dim oneCommune() As String
            Dim nbToponyme As Long
    
            Dim IDColumn As DataColumn = datatableToponymes.Columns.Add("IDColumn", GetType(String))
    
            ' Set the ID column as the primary key column.
            datatableToponymes.PrimaryKey = New DataColumn() {IDColumn}
            datatableToponymes.Columns.Add("Nom_commune", GetType(String))
            datatableToponymes.Columns.Add("Code_Postal", GetType(String))
            datatableToponymes.Columns.Add("Code_commune_INSEE", GetType(String))
            datatableToponymes.Columns.Add("Libelle_acheminement", GetType(String))
    
            srFichierCommunes = New StreamReader(Application.StartupPath & "\laposte_hexasmal.csv")
            Do While srFichierCommunes.Peek() >= 0
                'Reading each line
                oneLine = srFichierCommunes.ReadLine
                'Parse into the object
                '    Code_commune_INSEE;Nom_commune;Code_postal;Libelle_acheminement;Ligne_5
                '11220;MARSEILLETTE;11800;MARSEILLETTE;
                '11223;MAS DES COURS;11570;MAS DES COURS;
                oneCommune = oneLine.Split(";")
                If oneCommune.Length >= 4 And oneLine.Contains("commune") = False Then
                    If oneCommune.Length = 5 Then
                        datatableToponymes.Rows.Add(New Object() {nbToponyme, oneCommune(1) & " " & oneCommune(4), oneCommune(2), oneCommune(0), oneCommune(3)})
                    Else
                        datatableToponymes.Rows.Add(New Object() {nbToponyme, oneCommune(1), oneCommune(2), oneCommune(0), oneCommune(3)})
                    End If
                    nbToponyme += 1
                    
                End If
    
            Loop
            srFichierCommunes.Close()
            DataGridView1.DataSource = datatableToponymes
        End Sub

    and the search and display:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'Search by Zip
            
            Dim foundRows() As Data.DataRow
            foundRows = datatableToponymes.Select("Code_Postal='" & TextBox2.Text & "'")
            For Each oneFind As Data.DataRow In foundRows
                RichTextBox1.AppendText(oneFind("Code_Postal").ToString() & " " & oneFind("Nom_commune").ToString() & Environment.NewLine)
            Next
    
        End Sub
    It is less than a second to find the records based on the Zip Code...








    Wednesday, May 11, 2016 12:16 PM
  • Cyrille

    Thank you so much for delivering a quite complete solution!

    Regards

    Bernard


    Bernard Bouree

    Wednesday, May 11, 2016 3:49 PM
  • If it helped, please mark it as the solution and Vote on the posts.

    Merci

    • Marked as answer by BernardBouree Wednesday, May 11, 2016 4:45 PM
    Wednesday, May 11, 2016 4:06 PM
  • Cyrille

    Is there a way to automate the increase of the PrimaryIndex ?

    Bernard


    Bernard Bouree

    Wednesday, May 11, 2016 4:47 PM
  • Thanks for having marked the post.... could you mark the one with code instead...it makes it easier for people searching solutions...

    What do you mean automate the increase? in the code proposed it increases by 1 after each successful read of a Commune data.

    'this is the first column, and the PrimaryKey for the table

    datatableToponymes.PrimaryKey = New DataColumn() {IDColumn}

    later the first column is loaded with the index:

    datatableToponymes.Rows.Add(New Object() {nbToponyme, oneCommune(1), oneCommune(2), oneCommune(0), oneCommune(3)})

    and the index is increased:

    nbToponyme += 1


    Modified signature....



    Wednesday, May 11, 2016 4:54 PM