Click here to Skip to main content
11,642,216 members (64,215 online)
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
Comments
J{0}Y at 19-Feb-15 5:46am
   
this query is working ??
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 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  
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 6

create a datatable

                 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 dc.tree.AsEnumerable()
                     join table2 in dc.direct.AsEnumerable() on new { ID =               Convert.ToInt32(table1.ID) } equals new { ID = Convert.ToInt32(table2.id) }
                     select new
                     {
                         ID= (System.Int32)table1.id,
                         Name = (System.Int32)table1.name,
                         MobileNumber= (System.Int32)table1.mobilenumber,
                         Address= (System.Int32)table2.address,
                         FatherName= (System.Int32)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  
Comments
CHill60 at 19-Feb-15 6:48am
   
If you are going to post solutions to 2-year-old-resolved questions then it is a really good idea to point out why your solution is better than those already offered. Rather than just dumping some code that looks remarkably similar to Solution 1.
Does this even work? "table1.id" is not the same as "table1.ID"
J{0}Y at 20-Feb-15 3:12am
   
Ohk CHill60

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



Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 19 Feb 2015
Copyright © CodeProject, 1999-2015
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