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 9063857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:06:23+00:00 2026-06-16T16:06:23+00:00

strong text Thanks to Tim who provided me the code below in this other

  • 0

strong textThanks to Tim who provided me the code below in this other post: Join/merge two excel files in asp.net and display them in gridview.

My question in this new post is:
How can I reuse or add linq result to a dataset or datatabe so that I can query the new table and not the two separate tables? The join keyword gave me the possibility to join the two tables. Now I would like to use this joined table to run other queries on it.

I hope my question makes sense. If you need more info, please let me know.

DataSet ds = new DataSet("Stock");
using (var dbConnection = new OleDbConnection(connString))
using (var dbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", dbConnection))
using (var da = new OleDbDataAdapter(dbCommand))
{
    da.Fill(ds, "HWTypes");
}

using (var dbConnection = new OleDbConnection(stockConn))
using (var dbCommand = new OleDbCommand("SELECT * FROM [Stock_voorlopig$]", dbConnection))
using (var da = new OleDbDataAdapter(dbCommand))
{
    da.Fill(ds, "Stock");
}

var joined = from rType in ds.Tables["HWTypes"].AsEnumerable()
             join rStock in ds.Tables["Stock"].AsEnumerable()
             on rType.Field<string>("ProductID") equals rStock.Field<string>("Partno")
             select new
             {
                 ProductID = rType.Field<string>("ProductID")
                 // add the other columns you need here
             };


GridView1.DataSource = joined;
GridView1.DataBind();

Edit 1:

I am trying to do something like this:

private static DataTable JoinDataTablesWithLinq()
{

 //rest of the code from above
 return joined.CopyToDataTable();
}

But I am getting this error:

The type 'AnonymousType#1' cannot be used as type parameter 'T' in the generic type or method 'System.Data.DataTableExtensions.CopyToDataTable<T>(System.Collections.Generic.IEnumerable<T>)'. There is no implicit reference conversion from 'AnonymousType#1' to 'System.Data.DataRow'.

Any help?

  • 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-16T16:06:24+00:00Added an answer on June 16, 2026 at 4:06 pm

    I am trying to do something like this:

    private static DataTable JoinDataTablesWithLinq()
    {
    
     //rest of the code from above
     return joined.CopyToDataTable();
    }
    

    You cannot create a DataTable “on the fly”. You have selected an anonymous type which is not a IEnumerable<DataRow> which you need to use CopyToDataTable. So you either have to use a custom class and fill a List<MyClass> with all properties you need to persist it or you have to create a DataTable with all necessary DataColumns of the anonymous type and fill that.

    You see that it’s not that easy. There’s one dynamic approach though. But that requires reflection, therefore it’s not efficient at all.

    Here we go… (extract of my other answer)

    You could build your own CopyToDataTable that takes any kind of IEnumerable(not only DataRow)and returns a new DataTable:

    Here is the implementation (with help of MSDN):

    public class ObjectShredder<T> {
        private System.Reflection.FieldInfo[] _fi;
        private System.Reflection.PropertyInfo[] _pi;
        private System.Collections.Generic.Dictionary<string, int> _ordinalMap;
        private System.Type _type;
    
        // ObjectShredder constructor.
        public ObjectShredder() {
            _type = typeof(T);
            _fi = _type.GetFields();
            _pi = _type.GetProperties();
            _ordinalMap = new Dictionary<string, int>();
        }
    
        /// <summary>
        /// Loads a DataTable from a sequence of objects.
        /// </summary>
        /// <param name="source">The sequence of objects to load into the DataTable.</param>
        /// <param name="table">The input table. The schema of the table must match that 
        /// the type T.  If the table is null, a new table is created with a schema 
        /// created from the public properties and fields of the type T.</param>
        /// <param name="options">Specifies how values from the source sequence will be applied to 
        /// existing rows in the table.</param>
        /// <returns>A DataTable created from the source sequence.</returns>
        public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption? options) {
            // Load the table from the scalar sequence if T is a primitive type.
            if (typeof(T).IsPrimitive) {
                return ShredPrimitive(source, table, options);
            }
    
            // Create a new table if the input table is null.
            if (table == null) {
                table = new DataTable(typeof(T).Name);
            }
    
            // Initialize the ordinal map and extend the table schema based on type T.
            table = ExtendTable(table, typeof(T));
    
            // Enumerate the source sequence and load the object values into rows.
            table.BeginLoadData();
            using (IEnumerator<T> e = source.GetEnumerator()) {
                while (e.MoveNext()) {
                    if (options != null) {
                        table.LoadDataRow(ShredObject(table, e.Current), (LoadOption)options);
                    } else {
                        table.LoadDataRow(ShredObject(table, e.Current), true);
                    }
                }
            }
            table.EndLoadData();
    
            // Return the table.
            return table;
        }
    
        public DataTable ShredPrimitive(IEnumerable<T> source, DataTable table, LoadOption? options) {
            // Create a new table if the input table is null.
            if (table == null) {
                table = new DataTable(typeof(T).Name);
            }
    
            if (!table.Columns.Contains("Value")) {
                table.Columns.Add("Value", typeof(T));
            }
    
            // Enumerate the source sequence and load the scalar values into rows.
            table.BeginLoadData();
            using (IEnumerator<T> e = source.GetEnumerator()) {
                Object[] values = new object[table.Columns.Count];
                while (e.MoveNext()) {
                    values[table.Columns["Value"].Ordinal] = e.Current;
    
                    if (options != null) {
                        table.LoadDataRow(values, (LoadOption)options);
                    } else {
                        table.LoadDataRow(values, true);
                    }
                }
            }
            table.EndLoadData();
    
            // Return the table.
            return table;
        }
    
        public object[] ShredObject(DataTable table, T instance) {
    
            FieldInfo[] fi = _fi;
            PropertyInfo[] pi = _pi;
    
            if (instance.GetType() != typeof(T)) {
                // If the instance is derived from T, extend the table schema
                // and get the properties and fields.
                ExtendTable(table, instance.GetType());
                fi = instance.GetType().GetFields();
                pi = instance.GetType().GetProperties();
            }
    
            // Add the property and field values of the instance to an array.
            Object[] values = new object[table.Columns.Count];
            foreach (FieldInfo f in fi) {
                values[_ordinalMap[f.Name]] = f.GetValue(instance);
            }
    
            foreach (PropertyInfo p in pi) {
                values[_ordinalMap[p.Name]] = p.GetValue(instance, null);
            }
    
            // Return the property and field values of the instance.
            return values;
        }
    
        public DataTable ExtendTable(DataTable table, Type type) {
            // Extend the table schema if the input table was null or if the value 
            // in the sequence is derived from type T.            
            foreach (FieldInfo f in type.GetFields()) {
                if (!_ordinalMap.ContainsKey(f.Name)) {
                    // Add the field as a column in the table if it doesn't exist
                    // already.
                    DataColumn dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name]
                        : table.Columns.Add(f.Name, f.FieldType);
    
                    // Add the field to the ordinal map.
                    _ordinalMap.Add(f.Name, dc.Ordinal);
                }
            }
            foreach (PropertyInfo p in type.GetProperties()) {
                if (!_ordinalMap.ContainsKey(p.Name)) {
                    // Add the property as a column in the table if it doesn't exist
                    // already.
                    DataColumn dc = table.Columns.Contains(p.Name) ? table.Columns[p.Name]
                        : table.Columns.Add(p.Name, p.PropertyType);
    
                    // Add the property to the ordinal map.
                    _ordinalMap.Add(p.Name, dc.Ordinal);
                }
            }
    
            // Return the table.
            return table;
        }
    }
    

    Now you can add these extensions:

    public static class CustomLINQtoDataSetMethods {
        public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) {
            return new ObjectShredder<T>().Shred(source, null, null);
        }
    
        public static DataTable CopyToDataTable<T>(this IEnumerable<T> source,
                                                    DataTable table, LoadOption? options) {
            return new ObjectShredder<T>().Shred(source, table, options);
        }  
    }
    

    Voilà! Now CopyToDataTable works with any kind of IEnumerable<T> 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: [Display(Name = Plain text. <span class=\red strong\>Red
I have an Ajax timer, where I have the following code !!! strong text
strong text I have grids showing to user and want to not allow sorting
* strong text *what i want to do is from my breakline content to
public class Text extends JPanel { private String text; public Text() { this.setPreferredSize(new Dimension(20,20));
So I essentially need to do this: String text = line1\n; text += line2\n;
I have a string like: hello #this# is #some text string# text text I
$text = '<p width=50px; style=padding:0px;><strong style=padding:0;margin:0;>hello</strong></p><table style=text-align:center></table>'; $text_2 = preg_replace(/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i,'<$1$2>', $text); OUTPUT(i have given
Hello i was just wondering is there anyway to reverse this code so that
How can I do the following? For example I have this text (it's a

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.