LINQ - copy query to datatable - Asked By anjali on 24-Aug-11 08:40 AM

Am Having query as specified below
var   Query = (from t1 in xmlDS.Tables["pricing-sum"].AsEnumerable()
       join t2 in xmlDS.Tables["fax"].AsEnumerable() on t1.Field<Int32>("solution") equals t2.Field<Int32>("solution")
       join t3 in xmlDS.Tables["seg"].AsEnumerable() on t2.Field<Int32>("fax-Id") equals t3.Field<Int32>("fax-Id")
       join t4 in xmlDS.Tables["pax"].AsEnumerable() on t2.Field<Int32>("solution") equals t4.Field<Int32>("solution")
       join t5 in xmlDS.Tables["paxinfo"].AsEnumerable() on t4.Field<Int32>("paxId") equals t5.Field<Int32>("paxId")
       join t6 in xmlDS.Tables["pricing"].AsEnumerable() on t5.Field<Int32>("paxId") equals t6.Field<Int32>("paxId")
       join t7 in xmlDS.Tables["pricinginfo"].AsEnumerable() on t6.Field<Int32>("pricingId") equals t7.Field<Int32>("pricingId")
       join t8 in xmlDS.Tables["segment"].AsEnumerable() on t3.Field<Int32>("segments") equals t8.Field<Int32>("segments")
       where t8.Field<string>("departure") == depart && t8.Field<string>("arrival") == arrival
       select new
       {   
       f_Id=t2.Field<Int32>("fax_Id"),
       f_number = t8.Field<string>("fax-number"),      
       departure= t8.Field<string>("departure-airport"),
       arrival=t8.Field<string>("arrival-airport"),
      
       }
      );
      GridView2.Visible = true;
      GridView2.DataSource = Query;
      GridView2.DataBind();


in order pass this query for sorting I plan to use dataview sorting method for that I need the dataset which bind the above query.... I tried copy datatable as below

 DataTable dt1 = Query.CopyToDataTable(); 

but its not working

Ravi S replied to anjali on 24-Aug-11 08:56 AM
HI

Turn the query into an Append query.

1. Design view on the query
2. Query menu > Append Query
3. In the dialog that pops up, select the table to append to.


When you want to transfer data, doulbe-click the icon to run the operation

refer
http://www.mrexcel.com/forum/showthread.php?t=241760
Ravi S replied to anjali on 24-Aug-11 08:57 AM
HI

Sometimes you need the Linq query result as datatable (I need it today)
  
Use this:
  

 
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
    {
      if (query == null)
      {
        throw new ArgumentNullException("query");
      }
      IDbCommand cmd = ctx.GetCommand((IQueryable)query);
      System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
      adapter.SelectCommand = (System.Data.SqlClient.SqlCommand)cmd;
      DataTable dt = new DataTable("dataTbl");
      try
      {
        cmd.Connection.Open();
        adapter.FillSchema(dt, SchemaType.Source);
        adapter.Fill(dt);
      }
      finally
      {
        cmd.Connection.Close();
      }
      return dt;
    }
Devil Scorpio replied to anjali on 24-Aug-11 12:47 PM
Hi Anjali,

You can achieve ur goal by using CopyToDataTable method

The CopyToDataTable method accepts as input a query that can return rows from multiple DataTable or DataSet objects. The CopyToDataTable method will copy the data but not the properties from the source DataTable or DataSet objects to the returned DataTable. You will need to explicitly set the properties on the returned DataTable, such as Locale and TableName.

The following example queries the SalesOrderHeader table for orders after August 8, 2025 and uses the CopyToDataTable method to create a DataTable from that query. The DataTable is then bound to a BindingSource, which acts as proxy for a DataGridView.

C# Code:-
// Bind the System.Windows.Forms.DataGridView object
// to the System.Windows.Forms.BindingSource object.
dataGridView.DataSource = bindingSource;

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable orders = ds.Tables["SalesOrderHeader"];

// Query the SalesOrderHeader table for orders placed
// after August 8, 2001.
IEnumerable<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
    select order;

// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();

// Bind the table to a System.Windows.Forms.BindingSource object,
// which acts as a proxy for a System.Windows.Forms.DataGridView object.
bindingSource.DataSource = boundTable;
anjali replied to Devil Scorpio on 24-Aug-11 11:50 PM
I am using var query and select the rows using new keyword, there is no property for CopyToDataTable. Using Ienumerator I tried but no use....what is the other way for this