Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2004, 04:47 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Getting the Top 5 Rows from a datatable

Hi everyone! More questions of course!!

Lets say i have a populated datatable with 100 records.

Lets say one of the columns is "Date",

I know i can do the following:

Dim dv as dataView(_dt)
dv.Sort = "Date DESC"

which will sort the records by Date Desc, which is good, now i want to simply grab the top 5 records and return them such that those top 5 can be binded to a datalist! Do i put the top 5 in a new datatable? a dataview? what?

It seems so simple, yet i'm having problems with it!


 
Old May 20th, 2004, 08:47 PM
Authorized User
 
Join Date: Mar 2004
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

why don't you just simply use the sql script to get the top 5 record?

example:
SELECT TOP 5 date from tbl_date ORDER BY date DESC

 
Old May 20th, 2004, 11:11 PM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

well, yes, i know that seems like the obvious, but here is a simple scenario where i might want the top 5. Lets say i have a complex query that returns me 50 records of relevant info, and the information can be sorted 5 different ways, i simply just want to get the top 5 without re-inventing the complex query. I would rather take the query and just retreive a view or snapshot of the top 5 according to some criteria.

 
Old May 23rd, 2004, 07:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you define the requirement more explicitly? Are you saying that you want to sort the "top 5" results in 5 different ways? Isn't the "top 5" dependant on how you are sorting? Or is the criteria for determining the "top 5" based on some explicit single sort, and then you want to sort those 5 results?
 
Old May 24th, 2004, 08:00 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

Ok, lets say i have some complex algorithm populate a dataset that contains 1 table "TopCompanies" and it has 100 records of the top 100 companies in a given state. Now, Instead of re-inventing the wheel to calculate only the top 5 companies, i simply want to read in this xml file using the dataset.ReadXML(Path), and give it some sort criteria and select only the top 5. So for instance, lets say i wanted the top 5 money making companies, I would simply sort by Revenue DESC, then take only the top 5 from the list.

I know that if i do a dataview of a dataset, i can give it some sorting criteria, now i just need to select the top 5 from the list, not all 100. Is that more clear? thanks

Flyin

 
Old May 24th, 2004, 08:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Just curious, is 'TOP' an ANSI standard?

 
Old May 24th, 2004, 05:44 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

No, TOP is a MS-SQL feature. Some other DB system have a similar feature, in MySQL I think it's called "limit", but there is not SQL Language standard.

Flyin-
I'm sorry I can't be of more help. I haven't worked with the more involved bits of ADO.net yet. I usually let the database handle my sorting and limiting. But I see what you are driving at.

This is pretty klugey, but couldn't you perform the select on the datatable, then delete all the rows past the 5th before you bind the result? Messy, but it might get the job done and still be faster than re-querying.
 
Old May 25th, 2004, 08:30 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

thanks for the help peter, and everyone else, yes this is a quest i will have to find the answer for. If and when i do find it i'll post it back here for all to see just incase anyone runs into the problem! thanks

Flyin

 
Old May 25th, 2004, 08:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi flyin,

If I were you, I'd create a method that accepts a DataSet and a Sort String and returns a DataTable (or DataSet or view or whatever seems to make the most sense).
This method sets the Sort expression, extracts the first 5 rows by looping through its View (using DataRowView items for example) and then adds those rows to the return table / view whatever.

This way, you can bind your TOP 5 items like this:

MyDataGrid.DataSource = GetTop5(MyCompleteDataSet, "Date DESC")


HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 14th, 2004, 08:36 AM
Authorized User
 
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default

hey everyone, just incase you were wondering i found a temp solution that works.

It involves two features of the datatable:
1. Clone() which basically copies the schema of the table from the old table into the new.
2. ImportRow() This will copy one row from a table into a new one. So simple example of this in action.
Lets take imar's function name

Function GetTop5(dt, RowCount, _Start)
    Dim _table as DataTable
    _table = dt.Clone()
    For i = _Start To _Start + RowCount
        If i >= dt.Rows.Count Then
            Exit For
        Else
            _table.ImportRow(dt.Rows(i))
        End If
    Next

    Return _table
end function

there it is. If anyone has any suggestions for updates to this they are very welcomed!






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL to get all rows but top vjs2445 SQL Language 22 May 18th, 2007 06:44 AM
Backcolor for separate rows DataRow in Datatable i mikeka VS.NET 2002/2003 0 July 3rd, 2006 08:03 PM
Adding rows, AND column(s) to existing datatable cliffd64 VB.NET 2002/2003 Basics 1 August 18th, 2005 06:50 AM
Use Rows from a DataTable to Create Columns In oth indigolion Classic ASP Professional 0 August 5th, 2005 12:44 PM
adding rows to datatable forkhead ADO.NET 2 March 18th, 2004 01:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.