c# - Linq results to dataset -


i use entity framework linq data out of database. download data in excel. excellibrary works datasets. possible linq data in dataset, can data in excel?

i tried this, of course doesn't work.

    protected void btnexcelchecklistdownload_click(object sender, eventargs e)     {         dataset dstest = new dataset();         var db = new billingentities();         var query = (from u in db.v_checklist select u).asqueryable();         dstest =  (dataset)query.select(u => u.ncr_id).distinct();         excellibrary.datasethelper.createworkbook("myexcelfile.xls", dstest);     } 

you can write own todatatable extension using reflection. following. smaple might want extend per needs.

public static class converttodatatable {     public static datatable todatatable<t>(this ienumerable<t> enumerable)     {         if (enumerable == null) throw new argumentexception("enumerable");         var dt = new datatable();         var es = enumerable list<t> ?? enumerable.tolist();         var first = es.first();         if (first != null)         {             var props = first.gettype().getproperties();             foreach (var propertyinfo in props)             {                 if (!propertyinfo.propertytype.isclass || propertyinfo.propertytype.name.equals("string"))                 {                     dt.columns.add(new datacolumn(propertyinfo.name));                 }             }         }          foreach (var e in es)         {             var props = e.gettype().getproperties();             datarow dr = dt.newrow();             dt.rows.add(dr);             foreach (var propertyinfo in props)             {                 if (!propertyinfo.propertytype.isclass || propertyinfo.propertytype.name.equals("string"))                 {                     dr[propertyinfo.name] = propertyinfo.getvalue(e);                 }             }         }          return dt;     } 

Comments