locked
index of a n item in datatable RRS feed

  • Question

  • How can i get the index of an element in datatable which i got using the select method of datatable as 

    dim d as new datatable= 'some data source'

    dim dr() as datarow

    dr=dt.select("some condition")

    now i want to find the index of first or second item which i have retrieved in data row. i mean there index in data table with out use of loops as loops take a lot of time when there is huge data in data table.

     

    Sunday, April 11, 2010 12:16 PM

Answers

  • did you mean..

            Dim dt As New DataTable
            Dim dr() As DataRow
    
            dr = dt.Select("some expression")
    
            Dim index As Integer = dt.Rows.IndexOf(dr(0))

    ??


    Thanks

    "Feel the Force !"
    • Proposed as answer by Cor Ligthert Sunday, April 11, 2010 1:32 PM
    • Marked as answer by suhailshah Sunday, April 11, 2010 4:58 PM
    Sunday, April 11, 2010 1:10 PM

All replies

  • Where did you get the idea that loops take a lot of time?

    If you see some short code, then it often done behind the scene as well in a loop.

     But you can use Linq to DataSet

    But be aware that it uses most often behind the scene more code

    http://msdn.microsoft.com/en-us/vbasic/bb688086.aspx


    Success
    Cor
    Sunday, April 11, 2010 12:58 PM
  • The Select method returns an array of DataRow objects.  So assuming you are looking at a single DataRow and you want to see the value at a particular index then you could use the Item property.  As an argument you can supply the column name as a string, or you could provide the column index if you know it.

    Dim value As String = dr.Item("MyColumnName").ToString

    Dim value As String = dr.Item(0).ToString

    Notice that I am using ToString() function to get the value as a string.  This is because the Item property returns a non-specific Object type, which should be converted or cast to the desired type.  To learn more about this property see the following link -- there are also other arguments that can be used as well:

    http://msdn.microsoft.com/en-us/library/system.data.datarow.item(v=VS.80).aspx

    Also, Item is the default property of the DataRow class, so you can write the same code above and remove the ".Item" portion :)

    Sunday, April 11, 2010 1:05 PM
  • did you mean..

            Dim dt As New DataTable
            Dim dr() As DataRow
    
            dr = dt.Select("some expression")
    
            Dim index As Integer = dt.Rows.IndexOf(dr(0))

    ??


    Thanks

    "Feel the Force !"
    • Proposed as answer by Cor Ligthert Sunday, April 11, 2010 1:32 PM
    • Marked as answer by suhailshah Sunday, April 11, 2010 4:58 PM
    Sunday, April 11, 2010 1:10 PM
  • Will somebody check this code, the loop is at my computer more than 4 times quicker than than the indexOf, but maybe I did something wrong

    Module Module1
        Sub Main()
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn("test", GetType(System.Int32)))
            For i = 1 To 100000
                Dim dr = dt.NewRow
                dr.Item("test") = i
                dt.Rows.Add(dr)
            Next
            Dim st As New Stopwatch
            Dim index As Integer
            st.Reset()
            st.Start()
            Dim drc() = dt.Select("test = 99000")
            index = dt.Rows.IndexOf(drc(0))
            st.Stop()
            Console.WriteLine(index.ToString & " " & st.ElapsedMilliseconds.ToString)
            st.Reset()
            st.Start()
            For i = 0 To dt.Rows.Count - 1
                If dt.Rows(i) Is drc(0) Then
                    index = i
                End If
            Next
            Console.WriteLine(index.ToString & " " & st.ElapsedMilliseconds.ToString)
            Console.ReadLine()
        End Sub
    End Module
    

     


    Success
    Cor
    Sunday, April 11, 2010 1:41 PM
  • thanks i was looking for it
    Sunday, April 11, 2010 2:14 PM
  • i checked the code given by you. surely it was very helpfull. but when i analysed it by reversing the code as run the loop first and then use indexof() i found that loop is slower as 

    Module Module1

        Sub Main()

            Dim dt As New DataTable

            dt.Columns.Add(New DataColumn("test", GetType(System.Int32)))

            For i = 1 To 100000

                Dim dr = dt.NewRow

                dr.Item("test") = i

                dt.Rows.Add(dr)

            Next

            Dim st As New Stopwatch

            Dim index As Integer

            Dim drc() = dt.Select("test = 99000")

            st.Start()

            For i = 0 To dt.Rows.Count - 1

                If dt.Rows(i) Is drc(0) Then

                    index = i

                End If

            Next

            st.Stop()

            Console.WriteLine(index.ToString & " " & st.ElapsedMilliseconds.ToString)

            st.Reset()

            st.Start()

            index = dt.Rows.IndexOf(drc(0))

            st.Stop()

            Console.WriteLine(index.ToString & " " & st.ElapsedMilliseconds.ToString)

            st.Reset()

            Console.ReadLine()

        End Sub

    End Module

    Sunday, April 11, 2010 2:17 PM
  • You are right, I asked to check it because I was in doubt of this result.

    The select is in the first evaluation of my original code and it should be before that.

     


    Thanks
    Cor
    Sunday, April 11, 2010 3:30 PM