• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1729
  • Last Modified:

Random rows from datatable

Hi guys,,

Here I got a big problem I need your expert help with.

I have datatable that I make SQLQuerys of (to check stuff in a table), this datatable is rather big so I need to make is smaller.

1: I want to create a functions that takes a datatable and a procent value
2: The procent value is the value that's sets the size of the datatable.
3: the problem is that I want radon row numbers rfrom the orginal table

So if I have a table with 100 rows and the procent value is 10% I want the return datatable to be 10 rows.

But the big problem is that I don't want to 10 first rows I want random rows from the orginal  datatable

But can't solve this problem, need some help guys. :)
0
AWestEng
Asked:
AWestEng
  • 2
  • 2
1 Solution
 
vbturboCommented:
0
 
lorelogicCommented:
Try this query for returning rows:

SELECT TOP 10 * FROM someTable ORDER By NEWID()
 
Here is a link for other types of DBs:
http://www.petefreitag.com/item/466.cfm

0
 
AWestEngAuthor Commented:
but the data is in a datatable from a text fil
0
 
AWestEngAuthor Commented:
Here is the solution

    Private Function RandomDatatable(ByVal dt As DataTable, ByVal SizeInProcent As Integer) As DataTable
        Dim myDataTable As New DataTable("test")
        Dim column As DataColumn

        Dim generator As New Random()
        Dim x As Integer = 0

        For i As Integer = 0 To m_test_CSVcolumNames.Length - 1
            column = New DataColumn(m_test_CSVcolumNames(i))
            column.DataType = GetType(String)
            myDataTable.Columns.Add(column)
        Next

        Dim myRow As DataRow
        Dim intZise As Integer = 0

        intZise = CInt(dt.Rows.Count * (SizeInProcent / 100))

        For i As Integer = 0 To intZise - 1
            x = generator.Next(dt.Rows.Count)
            myRow = myDataTable.NewRow
            myRow.ItemArray = CType(dt.Rows(x).ItemArray.Clone, [Object]())
            myDataTable.Rows.Add(myRow.ItemArray)
        Next

        Return myDataTable
    End Function
0
 
vbturboCommented:
But if you looked at my PAQ then the only difference was counting down the rows  instead of divide with 10%

but glad you found your own solution.

vbturbo
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now