Introduction
In this article, I'll show how to get a random subset of data from a DataTable
. This is useful when you already have queries that are filtered correctly, but it returns all the rows.
Analysis
I came across this situation when I wanted to display a random tag cloud. I already had the query to get the keywords ordered by number of clicks and I wanted to created a tag cloud. Tags that are the most popular should have more chance to get picked and should be displayed larger than the less popular ones.
Implementation
In this code snippet, there is everything you need:
Private Const MIN_FONT_SIZE As Integer = 9
Private Const MAX_FONT_SIZE As Integer = 14
Public Shared Function GetTags() As MediasTagsDataTable
Dim dt As MediasTagsDataTable = taMediasTags.GetDataValide
If dt Is Nothing OrElse dt.Rows.Count < 1 Then
Return New MediasTagsDataTable
End If
Dim groupLenth As Integer = CType(Math.Floor_
(dt.Rows.Count / (MAX_FONT_SIZE - MIN_FONT_SIZE)), Integer)
Dim counter As Integer = 0
Dim groupCounter As Integer = 0
For Each row As MediasTagsRow In dt
row.c_FontSize = MIN_FONT_SIZE + groupCounter
counter += 1
If groupLenth <= counter Then
counter = 0
groupCounter += 1
End If
Next
Return dt
End Function
Public Shared Function GetRandomSampleTags(ByVal KeyCount As Integer) _
As MediasTagsDataTable
Dim dt As MediasTagsDataTable = GetTags()
Dim rep As MediasTagsDataTable = New MediasTagsDataTable
Dim count As Integer = 0
Dim rand As New Random()
While count < KeyCount
Randomize()
Dim r As Integer = rand.Next(0, dt.Rows.Count - 1)
Dim tmpRow As MediasTagsRow = dt(r)
rep.ImportRow(tmpRow)
dt.Rows.RemoveAt(r)
count += 1
End While
Return rep
End Function
Pros
This method is good because it doesn't require much work to get it working fast. It is a good concept when you are working with small tables, let's say less than 100 records.
Cons
If you have more than 100 records, an out of memory exception may occur since we are copying and duplicating rows. I would consider using a stored procedure instead.
Category: CodeProject
Published: 4/19/2009 10:04 PM
I am the general director of Nitriques Solutions inc. We build software and web solutions for enterprises using .NET 2.0 and 3.5, MS-SQL Server 2005 running on various Server 2003.
We offer a variety of products, from simple corporate web site with CMS to complete integration of intranets.
I have been working with asp et MS-SQL since 2002. I am particularly interested in web development 2.0, ajax, css, xhtml, asp.net, jQuery, Open Social and other web technologies.
In my spare time (when this concept exist in my life), i like to snowboard, skydive or free fall in wind tunnels and learning cool new stuff about anything.
Please feel free to contact me. http://www.nitriques.com