c# - Only parameterless constructors and initializers are supported in LINQ to Entities. (ASP.NET Web API) -


i have webapi controller generate xml table data

here code

  public class xmlcontroller : apicontroller {     private trackingappentities db = new trackingappentities();     [httpget]     public httpresponsemessage index()     {         var xdoc = new xdocument(             new xelement("data",                 db.timetables.select(w =>                     new xelement("worker",                         new xattribute("id", w.inn),                         new xelement("start", w.startday),                         new xelement("pause", w.startpause),                         new xelement("continue", w.endpause),                         new xelement("end", w.endday)                     )                 )             )         );          return new httpresponsemessage() { content = new stringcontent(xdoc.tostring(), encoding.utf8, "application/xml") };     } } 

here timetable class

 public partial class timetable {     public int id { get; set; }     public string company { get; set; }     public string inn { get; set; }     public string startday { get; set; }     public string startpause { get; set; }     public string endday { get; set; }     public string endpause { get; set; } } 

but when send request, have response

only parameterless constructors , initializers supported in linq entities.

how can fix this?

linq works inspecting code of function calls, rather result.

in case, solution call asenumerable tell linq populate each record sql, before calling constructor.

var timetables = db.timetables     /*      * select optional; improve performance table many       * properties, have little-to-no relevant impacts if you're      * using them all. similar select * versus selecting individual      * columns.      */     .select(c => new {         c.inn,         c.startday,         c.startpause,         c.endpause,         c.endday     })     .asenumerable(); // or ...await tolistasync();  var xdoc = new xdocument(         new xelement("data",             timetables.select(w =>                 new xelement("worker",                     new xattribute("id", w.inn),                     new xelement("start", w.startday),                     new xelement("pause", w.startpause),                     new xelement("continue", w.endpause),                     new xelement("end", w.endday)                 )             )         )     ); 

otherwise, can imagine linq trying send logic generating xelement directly sql. since sql wouldn't understand logic involved in process, linq throws.

in example, select call builds anonymous type. because it's guaranteed have no additional logic performed in constructor, linq able select values, , populate them c#-side.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -