Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
Hi Experts,

Thanks For attention.

C#
var results = from table1 in tree.AsEnumerable()
                     join 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"],

                     }


I want the var results convert into a DataTable.
Like:

DataTable dt=result;

Thanks in Advance For Help.
Posted
Comments
J{0}Y 19-Feb-15 5:46am    
this query is working ??

create a datatable with same structure of result
ie.

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 = from table1 in tree.AsEnumerable()
                             join 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);
               }
 
Share this answer
 
v4
Comments
Kaushik Saha from Kolkata,India 25-Sep-12 7:37am    
Thanks for reply.But Its not working.
kanha.460 25-Sep-12 7:50am    
its not showing
dt=results.copyToDataTable();
vasim sajad 25-Sep-12 7:55am    
plz check updated code
vasim sajad 25-Sep-12 7:57am    
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 = from table1 in tree.AsEnumerable()
join 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);
}
Hi,

See the below link.
It might be help you to solve your problem.

Convert Linq Query Result To DataTable[^]

Thanks,
Viprat
 
Share this answer
 
Hi,
Use this function:
C#
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
{
     if (query == null)
     {
          throw new ArgumentNullException("query");
     }
     
     IDbCommand cmd = ctx.GetCommand(query as IQueryable);
     SqlDataAdapter adapter = new SqlDataAdapter();
     adapter.SelectCommand = (SqlCommand)cmd;
     DataTable dt = new DataTable("sd");

     try
     {
          cmd.Connection.Open();
          adapter.FillSchema(dt, SchemaType.Source); 
          adapter.Fill(dt);
     }
     finally
     {
          cmd.Connection.Close();
     }
     return dt;
}


This code is given with the reference of this article[^].

Hope it helps.!
--Amit
 
Share this answer
 
SQL
Dim searchQuery = From dr In dtAccCode.AsEnumerable Where dr.Field(Of String)(1).StartsWith(txtSearch.Text) Select dr
dtSearchCode = searchQuery.CopyToDataTable()



Hope this will help to you
 
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