Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CopyToDataTable() only works when your query returns an IEnumerable&lt;'DataRow>. In your query, you are returning an anonymous type. Anonymous types don't carry the extension method for CopyToDataTable(). </p> <p>You can create a table using the ConvertToDataTable extension listed below. You'll have to convert it to VB.NET (there are converters out there if you google).</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Common; namespace TestConsole { public class Linq_join_2_datatables_that_share_a_column_and_put_result_in_new_datatable { public class Table1 { public int CatalogId { get; set; } public string Name { get; set; } public int SNum { get; set; } } public class Table2 { public int CatalogId { get; set; } public int NumItems { get; set; } public int IgnoredItems { get; set; } } public static void Start() { DataTable table1 = new DataTable(); table1.Columns.Add("catalogid", typeof(int)); table1.Columns.Add("name", typeof(string)); table1.Columns.Add("snum", typeof(int)); DataRow row = table1.Rows.Add(353, "xx", 4); DataTable table2 = new DataTable(); table2.Columns.Add("catalogid", typeof(int)); table2.Columns.Add("numitems", typeof(int)); table2.Columns.Add("ignoreditems", typeof(int)); table2.Rows.Add(353, 4, 0); var query = (from t1 in table1.AsEnumerable() join t2 in table2.AsEnumerable() on t1.Field&lt;int&gt;("catalogid") equals t2.Field&lt;int&gt;("catalogid") select new { catalogid = t1.Field&lt;int&gt;("catalogid"), name = t1.Field&lt;string&gt;("name"), snum = t1.Field&lt;int&gt;("snum"), numitems = t2.Field&lt;int&gt;("numitems"), ignoreditems = t2.Field&lt;int&gt;("ignoreditems") }).ToList(); DataTable table3 = query.ConvertToDataTable(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.ComponentModel; using System.Reflection; namespace Common { public static class DataTableExtensions { public static DataTable ConvertToDataTable&lt;T&gt;(this IList&lt;T&gt; data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable table = new DataTable(); foreach (PropertyDescriptor prop in properties) table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); foreach (T item in data) { DataRow row = table.NewRow(); foreach (PropertyDescriptor prop in properties) row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; table.Rows.Add(row); } table.AcceptChanges(); return table; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload