Random rows from datatable

Posted on 2007-07-29
Last Modified: 2013-11-26
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. :)
Question by:AWestEng
    LVL 18

    Accepted Solution

    LVL 3

    Expert Comment

    Try this query for returning rows:

    SELECT TOP 10 * FROM someTable ORDER By NEWID()
    Here is a link for other types of DBs:

    LVL 1

    Author Comment

    but the data is in a datatable from a text fil
    LVL 1

    Author Comment

    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)

            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]())

            Return myDataTable
        End Function
    LVL 18

    Expert Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now