{"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"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Select Top 10 records from a DataTable in ADO.NET - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"Select Top 10 records from a DataTable in ADO.NET\",\"datePublished\":\"2006-10-30T23:12:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/\"},\"wordCount\":438,\"commentCount\":0,\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/\",\"name\":\"Select Top 10 records from a DataTable in ADO.NET - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"datePublished\":\"2006-10-30T23:12:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2006\\\/10\\\/30\\\/select-top-10-records-from-a-datatable-in-adonet\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Select Top 10 records from a DataTable in ADO.NET - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"Select Top 10 records from a DataTable in ADO.NET","datePublished":"2006-10-30T23:12:00+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/"},"wordCount":438,"commentCount":0,"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/","name":"Select Top 10 records from a DataTable in ADO.NET - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"datePublished":"2006-10-30T23:12:00+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2006\/10\/30\/select-top-10-records-from-a-datatable-in-adonet\/"]}]},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_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}]}}