asp.net - convert linq query results to datatable C# -


i want convert linq query results datatable can assign datatable gridview show on asp page.

however not able convert results datatable, not getting copytotable() method in code.

please advise doing wrong here?

 var gradedata = (from data in oangiectxt.prc_shopinstancecustomersdata(convert.toint32(this.shopinstanceid), 10000, false)                      .where( row => row.recievedpoints != "n/a" )                     .groupby(row => new { row.name })                     .select(g => new groupedpoints()                     {                         name = g.key.name,                         totalpoints = g.sum(x => convert.todouble(x.recievedpoints) * (x.weightage.tostring() == "0.00" ? 1 : convert.todouble(x.weightage)))                     })                      select data).tolist();   datatable dt = gradedata --gradedata.copytotable() 

note: reference dataextentions dll available.

thanks in advance

you should datatableextensions.copytodatatable

remove tolist().

copytodatatable ienumerable<datarow> extension (unfortunately).

there solution custom copytodatatable extension method below.

var gradedata = (from data in oangiectxt.prc_shopinstancecustomersdata(                  convert.toint32(this.shopinstanceid), 10000, false)                 .where( row => row.recievedpoints != "n/a" )                 .groupby(row => new { row.name })                 .select(g => new                 {                     name = g.key.name,                     totalpoints = g.sum(x => convert.todouble(x.recievedpoints)                      * (x.weightage.tostring() == "0.00" ? 1                        : convert.todouble(x.weightage)))                 })                  select data);  var dt = gradedata.copytodatatable(); 

edit:

here more useful implementation of copytodatatable there no type constraint datarow.

  public static class datasetlinqoperators   {     public static datatable copytodatatable<t>(this ienumerable<t> source)     {         //you find objectshredder implementation on blog wich linked.         return new objectshredder<t>().shred(source, null, null);     }      public static datatable copytodatatable<t>(this ienumerable<t> source,                                       datatable table, loadoption? options)     {         return new objectshredder<t>().shred(source, table, options);     }    } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -