Click here to Skip to main content
15,888,158 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi All,


how to get data from linq query to datable using c#.net , can u guide me or send snippets
Posted
Comments
Pheonyx 9-Jul-14 7:06am    
What have you tried? What have you googled/looked up?
jo.him1988 9-Jul-14 7:08am    
hi hope this will work for you
DamithSL 9-Jul-14 7:09am    

C#
DataTable workTable = new DataTable();
workTable.Columns.Add("ContryID", typeof(Int32));
workTable.Columns.Add("CountryName", typeof(string));
Data.TestEntities ent = new Data.TestEntities();
var country = ent.Countries.ToList();
foreach (var item in country)
{
    DataRow workRow = workTable.NewRow();
    workRow["ContryID"] = item.CountryID;
    workRow["CountryName"] = item.CountryName;
    workTable.Rows.Add(workRow);
}

now you can do anuthing with your workTable

happy coding :) :) :)
 
Share this answer
 
v2
Comments
kingsa 9-Jul-14 7:29am    
Thanks Withlooping is it possible to get data from object to table
I think no need to get data from .edmx to data table, List/Generic list is light weight and you should use the same.

But if you want
you can copy data from list to Data-Table by foreach as in above answer.


Regards,
Manoj
 
Share this answer
 
v2

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