Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I am new in linq so want to know that how to get linq query data into datatable. I am using visual studio 2008. I have tried some solution but can not access CopyToDatable() into code. I have to perform edit in gridview using linq in rows itself on editindexing. I have to edit dropdown which is city field.
Posted
Updated 10-Sep-12 0:11am
v4
Comments
SruthiR 10-Sep-12 5:29am    
Please post your code here.

Your select statement is returning a sequence of strings (IEnumerable or IQueryable), not a sequence of DataRows. CopyToDataTable() is only available on IEnumerable<t></t> where T is or derives from DataRow.
So, instead of select new {...} use select obj. Try this:
C#
var query = from obj in dt.AsEnumerable() where obj.Field<string>("somefield") == "test"
            select obj;
if(query.Count() > 0)
{
    DataTable dt = query.CopyToDataTable();
}



--Amit
 
Share this answer
 
v2
Comments
ravi sharma11 10-Sep-12 6:50am    
I can not access query.CopyToDataTable()
can not find .CopyToDataTable() this.
_Amy 10-Sep-12 6:53am    
Refer this[^] link.
hi,
if u want datable in linq for that you can use Dataset in linq by choosing

select your project --->click on newitem -->select New Item --->Select DataTemplate ---->Select DataSet --->Click on ok.
Now dataset comes into your template.
then connect to your database


then convert from data set to datatable using below syntax :



DataSet ds = new DataSet();
DataTable dT = ds.Tables["TableName"];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900