.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

How can I convert var(resultset) in to Datatable(when some columns are selecting in linq Query)

Posted By: Pankaj Dhingra     Posted Date: April 08, 2014    Points:5   Category :C#
DataTable dtN = new DataTable();

dtN.Columns.Add("Id");
dtN.Columns.Add("Name");
dtN.AcceptChanges();

for (int i = 1; i <= 10; i++)
{
DataRow dr = dtN.NewRow();
dr["Id"] = i;
dr["Name"] = "A"+i.ToString();
dtN.Rows.Add(dr);
dtN.AcceptChanges();
}

var data = from r in dtN.AsEnumerable()

select new {Name=r["Name"].ToString() };

DataTable dt = data.CopyToDataTable();
//this line is giving error because CopyToDatatable is not there

Responses
Author: Akhil Raj       [Moderator]      
Posted Date: May 19, 2014     Points: 20   

Hi,
Actually you are return the result as anonymous type from the linq. The CopyToDatabase() works with Enumerable<DataRow>. You need to cast that or use specific conversion class.

The following is the same question with answer

http://stackoverflow.com/questions/12437207/not-having-copytodatatable-method



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend