{"id":4393,"date":"2008-05-13T21:35:00","date_gmt":"2008-05-13T21:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vbteam\/2008\/05\/13\/using-linq-to-dataset-in-an-aspx-page-jonathan-aneja\/"},"modified":"2024-07-05T14:25:23","modified_gmt":"2024-07-05T21:25:23","slug":"using-linq-to-dataset-in-an-aspx-page-jonathan-aneja","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/vbteam\/using-linq-to-dataset-in-an-aspx-page-jonathan-aneja\/","title":{"rendered":"Using LINQ to Dataset in an .aspx page (Jonathan Aneja)"},"content":{"rendered":"<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Recently I got a customer question about how to use <a class=\"\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb386977.aspx\">LINQ to Dataset<\/a> in an .aspx file.<span>&nbsp; <\/span>The compiler was complaining that it couldn&#8217;t find the AsEnumerable method that allows LINQ to work over a DataTable (&#8220;AsEnumerable is not a member of &#8216;DataTable'&#8221;).<span>&nbsp; <\/span>The code he sent looks correct, so why is the compiler not picking up the extension method (defined in System.Data.DataSetExtensions.dll)?<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><span>&lt;%<\/span><span>@<\/span><span> <span>Page<\/span> <span>Language<\/span><span>=&#8221;vb&#8221;<\/span> <span>AutoEventWireup<\/span><span>=&#8221;false&#8221;<\/span> <span>CodeBehind<\/span><span>=&#8221;Default.aspx.vb&#8221;<\/span> <span>Inherits<\/span><span>=&#8221;WebApplication6._Default&#8221;<\/span> <span>%&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&lt;%<\/span><span>@<\/span><span> <span>Import<\/span> <span>Namespace<\/span><span>=&#8221;System.Linq&#8221;<\/span> <span>%&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>&lt;%<\/span><span>@<\/span><span> <span>Import<\/span> <span>Namespace<\/span><span>=&#8221;System.Data.DataSetExtensions&#8221;<\/span> <span>%&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&lt;<\/span><span>script<\/span><span> <span>runat<\/span><span>=&#8221;server&#8221;&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>Sub<\/span> Page_Load(sender <span>As<\/span> <span>Object<\/span>,&nbsp;e <span>As<\/span> EventArgs) <span>Handles<\/span> <span>Me<\/span>.Load<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span><\/span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> ds <span>As<\/span> <span>New<\/span> System.Data.DataSet<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>ds.ReadXml(Server.MapPath(<span>&#8220;.\/downloads\/somedatafile.xml&#8221;<\/span>))<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p><span><span><span><font color=\"#000000\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/font><\/span><\/span>&#8216;error here<\/p>\n<p><\/span><\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>Dim<\/span> query = <span>From<\/span> row <span>In<\/span> ds.Tables(0).AsEnumerable() <span>Select<\/span> row <\/span><\/p>\n<p class=\"MsoNormal\"><span><span><\/span><\/span>&nbsp;<\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp; <\/span><span>End<\/span> <span>Sub<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>&lt;\/<\/span><span>script<\/span><span>&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">After staring at that for a while and thinking it was a bug in the compiler, I forwarded it on to some of the ADO guys.<span>&nbsp; <\/span>It turns out the fix is simple, change the import to say &ldquo;System.Data&rdquo; instead of &ldquo;System.Data.DataSetExt\nensions&rdquo;.<span>&nbsp; <\/span>If you open up Reflector the reason&rsquo;s obvious: the AsEnumerable extension method is defined in the System.Data <i>namespace<\/i> of the System.Data.DatasetExtensions <i>assembly<\/i>.<span>&nbsp; <\/span>There is a System.Data.DatasetExtensions namespace as well, but it&rsquo;s not the one that contains the extension methods.<\/font><\/font><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\"><\/font><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">I can see why they&nbsp;designed it this way,&nbsp;by putting it in System.Data you can use&nbsp;LINQ right out of the box on your existing Datasets, since typically System.Data is already imported.&nbsp; Unfortunately that wasn&#8217;t the case here.<\/font><\/font><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Here&rsquo;s the right line of code to use:<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><span>&lt;%<\/span><span>@<\/span><span> <span>Import<\/span> <span>Namespace<\/span><span>=&#8221;System.Data&#8221;<\/span> <span>%&gt;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">As an aside, you don&rsquo;t actually have to insert the explicit call to AsEnumerable when working with LINQ to Dataset (in VB), I&rsquo;ll explain why in a blog post tomorrow.<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">Jonathan<\/p>\n<p><\/font><\/font><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I got a customer question about how to use LINQ to Dataset in an .aspx file.&nbsp; The compiler was complaining that it couldn&#8217;t find the AsEnumerable method that allows LINQ to work over a DataTable (&#8220;AsEnumerable is not a member of &#8216;DataTable&#8217;&#8221;).&nbsp; The code he sent looks correct, so why is the compiler not [&hellip;]<\/p>\n","protected":false},"author":260,"featured_media":8818,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[192,195],"tags":[67,83,94,117,166,178],"class_list":["post-4393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-featured","category-visual-basic","tag-extension_methods","tag-jonathan-aneja","tag-linqvb9","tag-orcas","tag-vb2008","tag-web"],"acf":[],"blog_post_summary":"<p>Recently I got a customer question about how to use LINQ to Dataset in an .aspx file.&nbsp; The compiler was complaining that it couldn&#8217;t find the AsEnumerable method that allows LINQ to work over a DataTable (&#8220;AsEnumerable is not a member of &#8216;DataTable&#8217;&#8221;).&nbsp; The code he sent looks correct, so why is the compiler not [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/users\/260"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/comments?post=4393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/posts\/4393\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media\/8818"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/media?parent=4393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/categories?post=4393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/vbteam\/wp-json\/wp\/v2\/tags?post=4393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}