c# - Unexpected Result while joining two datatables? -


 object combinedrows = dt1 in dsresults.tables[0].asenumerable()                            join dt2 in dsresults.tables[1].asenumerable() on dt1.field<string>("methodname") equals dt2.field<string>("methodname")                            select new { dt1, dt2 };         datatable finaldt = new datatable("finaltable");         finaldt.columns.add(new datacolumn("sp",typeof(string)));         finaldt.columns.add(new datacolumn("method",typeof(string)));         finaldt.columns.add(new datacolumn("class",typeof(string)));         finaldt.columns.add(new datacolumn("bllmethod",typeof(string)));         datarow newrow = finaldt.newrow();         finaldt.rows.add((datarow)combinedrows);         datagridview5.datasource = finaldt; 

the above coding gives result in first column follows: system.linq.enumerable+d__614[system.data.datarow,system.data.datarow,system.string,<>f__anonymoustype02[system.data.datarow,system.data.datarow]]

@prem: after understanding code sure exception like

"unable cast object of type 'd__614[system.data.datarow,system.data.datarow,system.string,<>f__anonymoustype02[system.data.datarow,system.data.datarow]]' type 'system.data.datarow'."

on

finaldt.rows.add((datarow)combinedrows); line

so must store linq return result in var , can add row new datatable loop.

your code should be

      var combinedrows = dt1 in dsresults.tables[0].asenumerable()                               join dt2 in dsresults.tables[1].asenumerable() on             dt1.field<string>("methodname") equals dt2.field<string>("methodname")                               select new { dt1, dt2 };          datatable finaldt = new datatable("finaltable");         finaldt.columns.add(new datacolumn("sp", typeof(string)));         finaldt.columns.add(new datacolumn("method", typeof(string)));         finaldt.columns.add(new datacolumn("class", typeof(string)));         finaldt.columns.add(new datacolumn("bllmethod", typeof(string)));         datarow newrow = finaldt.newrow();                    foreach (var row in combinedrows)         {              datarow datarow = finaldt.newrow();             datarow.itemarray = row.dt1.itemarray;               finaldt.rows.add(datarow);         } 

try out on behalf of have checked running if not post error.

for particular column datatable need change linq like

 var combinedrows = dt1 in dsresults.tables[0].asenumerable()                               join dt2 in dsresults.tables[1].asenumerable() on dt1.field<string>("methodname") equals dt2.field<string>("methodname")                            select new { td1col = dt1.field<string>("tab1col2")};  

and retrieving data need do:

       foreach (var row in combinedrows)         {             string value = row.td1col.tostring();                                         } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -