Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi Experts,
 
Thanks For attention.

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 25-Sep-12 1:13am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
                }
  Permalink  
v4
Comments
Kaushik Saha from Kolkata,India at 25-Sep-12 7:37am
   
Thanks for reply.But Its not working.
kanha.460 at 25-Sep-12 7:50am
   
its not showing
dt=results.copyToDataTable();
vasim sajad at 25-Sep-12 7:55am
   
plz check updated code
vasim sajad at 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);
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
See the below link.
It might be help you to solve your problem.
 
Convert Linq Query Result To DataTable[^]
 
Thanks,
Viprat
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
Use this function:
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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 310
1 Sergey Alexandrovich Kryukov 166
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100