Convert LinQ var into a DataTable in C# getting 'specified cast is not valid' Exception -
Convert LinQ var into a DataTable in C# getting 'specified cast is not valid' Exception -
i'm trying load linq datatable in c#. used below code while compiling i'm getting specified cast not valid
exception.
datatable dt = new datatable(); dt.columns.add("id", typeof(int)); dt.columns.add("name", typeof(int)); dt.columns.add("mobilenumber", typeof(int)); dt.columns.add("address", typeof(int)); dt.columns.add("fathername", typeof(int)); var results = table1 in tree.asenumerable() bring together table2 in direct.asenumerable() on (int)table1["id"] equals (int)table2["id"] select new { id = (int)table1["id"], name = (int)table1["name"], mobilenumber = (int)table1["mobilenumber"], address = (int)table2["address"], fathername = (int)table2["fathername"], }; foreach (var item in results) { datarow dr = dt.newrow(); dr["id"] = item.id; dr["name"] = item.name; dr["mobilenumber"] = item.mobilenumber; dr["address"] = item.address; dr["fathername"] = item.fathername; dt.rows.add(dr); }
all columns of type int. assume name, mobile number, , fathername string.
c# linq dataset
Comments
Post a Comment