<p>How do you expose a LINQ query as an ASMX web service?
<br>
Usually, from the business tier, I can return a typed <code>DataSet</code> or a <code>DataTable</code> which can be serialized for transport over ASMX.</p>
<p>How can I do the same for a LINQ query? <br>
Is there a way to populate a typed <code>DataSet</code> or a <code>DataTable</code> via a LINQ query?</p>
<pre><code>public static MyDataTable CallMySproc()
{
    string conn = &quot;...&quot;;

    MyDatabaseDataContext db = new MyDatabaseDataContext(conn);
    MyDataTable dt = new MyDataTable();

    // execute a sproc via LINQ
    var query = from dr
                in db.MySproc().AsEnumerable
                select dr;

    // copy LINQ query resultset into a DataTable -this does not work !    
    dt = query.CopyToDataTable();
   
    return dt;
}
</code></pre>
<p>How could I put the result set of a LINQ query into a <code>DataSet</code> or a <code>DataTable</code>?
<br>
Alternatively, can the LINQ query be serializable so that I can expose it as an ASMX web service?</p>

[{'answer_id': 84, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 4, 'selected': False, 'text': '<p>If you use a return type of <code>IEnumerable</code>, you can return your <strong>query</strong> variable directly.</p>\n'}, {'answer_id': 5830, 'author': 'Brian Childress', 'author_id': 721, 'author_profile': 'https://Stackoverflow.com/users/721', 'pm_score': 4, 'selected': False, 'text': '<p>Create a class object and return a <code>list(T)</code> of the query.</p>\n'}, {'answer_id': 12446, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 8, 'selected': True, 'text': '<p>As mentioned in the question, <code>IEnumerable</code> has a <code>CopyToDataTable</code> method:</p>\n\n<pre><code>IEnumerable&lt;DataRow&gt; query =\n    from order in orders.AsEnumerable()\n    where order.Field&lt;DateTime&gt;("OrderDate") &gt; new DateTime(2001, 8, 1)\n    select order;\n\n// Create a table from the query.\nDataTable boundTable = query.CopyToDataTable&lt;DataRow&gt;();\n</code></pre>\n\n<p>Why won\'t that work for you?</p>\n'}, {'answer_id': 12460, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 5, 'selected': False, 'text': '<p>Make a set of Data Transfer Objects, a couple of mappers, and return that via the .asmx.<br>\nYou should <em>never</em> expose the database objects directly, as a change in the procedure schema will propagate to the web service consumer without you noticing it.</p>\n'}, {'answer_id': 544169, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>To perform this query against a <code>DataContext</code> class, you'll need to do the following:</p>\n\n<pre><code>MyDataContext db = new MyDataContext();\nIEnumerable&lt;DataRow&gt; query = \n    (from order in db.Orders.AsEnumerable()\n        select new\n        {\n            order.Property,\n            order.Property2\n        })\n    as IEnumerable&lt;DataRow&gt;;\nreturn query.CopyToDataTable&lt;DataRow&gt;();\n</code></pre>\n\n<p>Without the <code>as IEnumerable&lt;DataRow&gt;;</code> you will see the following compilation error:</p>\n\n<blockquote>\n  <p>Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)</p>\n</blockquote>\n"}, {'answer_id': 49774484, 'author': 'Vijay S', 'author_id': 7204125, 'author_profile': 'https://Stackoverflow.com/users/7204125', 'pm_score': 3, 'selected': False, 'text': '<p>If you use <code>IEnumerable</code> as the return type, it will return your query variable directly.</p>\n<pre><code>MyDataContext db = new MyDataContext();\nIEnumerable&lt;DataRow&gt; query = \n    (from order in db.Orders.AsEnumerable()\n        select new\n        {\n            order.Property,\n            order.Property2\n        })\n    as IEnumerable&lt;DataRow&gt;;\nreturn query.CopyToDataTable&lt;DataRow&gt;();\n</code></pre>\n'}, {'answer_id': 56693278, 'author': 'Gabriel Magana', 'author_id': 99455, 'author_profile': 'https://Stackoverflow.com/users/99455', 'pm_score': 2, 'selected': False, 'text': '<p>For the sake of completeness, these solutions do not work for EF Core (at least not for EF Core 2.2). Casting to <code>IEnumerable&lt;DataRow&gt;</code>, as suggested in the other answers here, fails. Implementing this class and extension methods worked for me <a href="https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/implement-copytodatatable-where-type-not-a-datarow" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/implement-copytodatatable-where-type-not-a-datarow</a>.</p>\n\n<p>Why it\'s not built into EF Core, I have not idea.</p>\n'}]
['https://Stackoverflow.com/questions/16', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2/']