Fill a DataSet or a DataTable from a LINQ query resultset
I create a function which take query of line and return datatable.
 public DataTable LINEToDataTable<T>(IEnumerable<T> varlist)
{
   DataTable dtReturn = new DataTable();
Â
   // column namesÂ
   PropertyInfo[] oProps = null;
Â
   if (varlist == null) return dtReturn;
Â
   foreach (T rec in varlist)
   {
     // Use reflection to get property names, to create table, Only first time, othersÂ
   Â
     if (oProps == null)
     {
        oProps = ((Type)rec.GetType()).GetProperties();
        foreach (PropertyInfo pi in oProps)
        {
          Type colType = pi.PropertyType;
Â
          if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
          ==typeof(Nullable<>)))
           {
             colType = colType.GetGenericArguments()[0];
           }
Â
          dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
        }
     }
Â
     DataRow dr = dtReturn.NewRow();
Â
     foreach (PropertyInfo pi in oProps)
     {
        dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue
        (rec,null);
     }
Â
     dtReturn.Rows.Add(dr);
   }
   return dtReturn;
}
Â
Â
Regards,
Â
desh rathore