Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:53:48+00:00 2026-06-12T03:53:48+00:00

I have the following datatables table1 : +———–+——+——+ | catalogid | name | snum

  • 0

I have the following datatables

table1:

+-----------+------+------+
| catalogid | name | snum |
+-----------+------+------+
|       353 | xx   |    4 |
|       364 | yy   |    3 |
|       882 | zz   |    3 |
|       224 | mm   |   71 |
|       999 | kk   |  321 |
|        74 | kk   |    4 |
|        54 | ii   |    5 |
|        11 | u    |    6 |
|        23 | yy   |    6 |
+-----------+------+------+

table2:

+-----------+----------+--------------+
| catalogid | numitems | ignoreditems |
+-----------+----------+--------------+
|       353 |        4 |            0 |
|       364 |       10 |            0 |
|       882 |        2 |            0 |
|       224 |        0 |            7 |
+-----------+----------+--------------+

Using LINQ I want to join them and copy the result to a new datatable. They both share catalogid, and in the result it should only display the records that their catalogid exist in table2

result:

+-----------+------+------+-----------+---------------+
| catalogid | name | snum | numitems  | ignoreditems  |
+-----------+------+------+-----------+---------------+
|       353 | xx   |    4 |         4 |             0 |
|       364 | yy   |    3 |        10 |             0 |
|       882 | zz   |    3 |         2 |             0 |
|       224 | mm   |   71 |         0 |             7 |
+-----------+------+------+-----------+---------------+

Here is my attempt but it’s not working:

 Dim query = From a In oresult.AsEnumerable
             Group Join b In products.AsEnumerable
             On a.Field(Of Integer)("catalogid") Equals b.Field(Of Integer)("catalogid")
             Into Group
 query.copytodatatable

CopyToDatatable is not working, and I can’t figure out why

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:53:50+00:00Added an answer on June 12, 2026 at 3:53 am

    CopyToDataTable() only works when your query returns an IEnumerable<‘DataRow>. In your query, you are returning an anonymous type. Anonymous types don’t carry the extension method for CopyToDataTable().

    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).

    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<int>("catalogid") equals t2.Field<int>("catalogid")
                            select new
                            {
                                catalogid = t1.Field<int>("catalogid"),
                                name = t1.Field<string>("name"),
                                snum = t1.Field<int>("snum"),
                                numitems = t2.Field<int>("numitems"),
                                ignoreditems = t2.Field<int>("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<T>(this IList<T> 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;
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sql tables oitems table +---------+-----------+----------+ | orderid | catalogid |
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have the following piece of code: DataRow CreateRow(DataTable dt, string name, string country)
I have the following lines of code in C# that gets data using DataTables
I have two DataTables which has the following rows. Table A: TaxID TaxName ------
I have the following code that fills dataTable1 and dataTable2 with two simple SQL
I have the following code that serializes a DataTable to XML. StringWriter sw =
I have converted my Datatable to json string use the following method... public string
I have a DataTable which is bound to datagridview (Winforms)... I use the following
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.