{"id":1134,"date":"2006-10-30T18:12:00","date_gmt":"2006-10-30T23:12:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=1134"},"modified":"2006-10-30T18:12:00","modified_gmt":"2006-10-30T23:12:00","slug":"select-top-10-records-from-a-datatable-in-adonet","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/","title":{"rendered":"Select Top 10 records from a DataTable in ADO.NET"},"content":{"rendered":"<p><P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">As far as I know, there is no way to perform a SELECT TOP 10 query on a disconnected Recordset in ADO.NET. However, you can create a DataView over the target table and use the technique described below to extract Top 10 records. <SPAN style=\"mso-spacerun: yes\">&nbsp;<\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">As you can see in the below excerpt, I am trying to return Top 10 records of my ds dataset using a method called \u201cGetTopTen\u201d. First of all, I have assigned the defaultView of the only DataTable in input dataset named &#8220;SITE_DT&#8221; to a DataView object and then have sorted the result based on &#8220;Total_Size&#8221; field. This guarantees that our result (no matter how many records is returned) are always sorted in a descending manner.<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt\">Next, I have cloned the schema of first DateTable to another DataTable and added the new DataTable to a completely new DataSet. ADO.NET normally does not let you add a DataTable (neither a DataRow) directly from one dataset to another, so you have to clone it&nbsp;in a way. Finally I&#8217;ve just grunt it out the old-fashioned way by looping through each row of the DataView object and adding them to a new DataTable. My method ends by returning the new dataset which contains the Top 10 records based on &#8220;Total_Size&#8221;. This might not be the best option for you, but perfectly works in my case.<\/P><br \/>\n<P><?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" \/><o:p><\/o:p><\/P><br \/>\n<P>public DataSet GetTopTen(DataSet ds)<\/P><br \/>\n<P>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Try<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataView dwDs= ds.Tables[&#8220;SITE_DT&#8221;].DefaultView;<BR>&nbsp;&nbsp;&nbsp;&nbsp;dwDs.Sort=&#8221;Total_Size DESC&#8221;;<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataSet dss&nbsp; = new DataSet();<BR>&nbsp;&nbsp;&nbsp;&nbsp;DataTable dt = dwDs.Table.Clone();<BR>&nbsp;&nbsp;&nbsp;&nbsp;dss.Tables.Add(dt);<\/P><br \/>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;int counter = 0;<\/P><br \/>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;if(dwDs.Count &gt; 10)&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter = 10;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter = dwDs.Count;<\/P><br \/>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i &lt; counter; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataRow dv = dwDs[i].Row;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dss.Tables[0].ImportRow(dv);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/P><br \/>\n<P>&nbsp;&nbsp;&nbsp;&nbsp;dss.AcceptChanges();<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(dss);<\/P><br \/>\n<P>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;catch(Exception e)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;throw e;<BR>&nbsp;&nbsp;&nbsp;}<\/P><br \/>\n<P>}<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As far as I know, there is no way to perform a SELECT TOP 10 query on a disconnected Recordset in ADO.NET. However, you can create a DataView over the target table and use the technique described below to extract Top 10 records. &nbsp; As you can see in the below excerpt, I am trying [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1134","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=1134"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1134\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=1134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=1134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=1134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}