Convert a LINQ Resultset to a DataTable

public DataTable LINQToDataTable(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("table");
     try     {
          cmd.Connection.Open();
          adapter.FillSchema(dt,
SchemaType.Source);
          adapter.Fill(dt);
     }
     finally     {
          cmd.Connection.Close();
     }     return dt;
}

---------------------------------------------------------------
Example: To use this method, just use the following code sample:
---------------------------------------------------------------
var vrCountry = from country in objEmpDataContext.CountryMaster
                        select new {country.CountryID,country.CountryName};
DataTable dt = LINQToDataTable(objEmpDataContext,vrCountry);

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender