c# - The entity or complex type 'Ws.Models.XXXX' cannot be constructed in a LINQ to Entities query -


i working along mvc4 webapi project. doing generating web service using dto(samplemodeldto) follows.

namespace ws.models {     public class samplemodeldto     {         [key]         public int id { set; get; }          public string productname { get; set; }          public int productquantity { get; set; }          public string productreview { get; set; }             } } 

this dto used in controller follows, fetching data 2 tables. have searched lot, used anonymous object it's still throwing same inner exception.

also searched here, still unable find solution. guidance highly appreciated.cheers.

public iqueryable<samplemodeldto> getproducts() {     var query = t in db.products                 join p in db.ratings on t.product_id equals p.prod_id                 select new samplemodeldto()                 {                     productname = t.product_name,                     productquantity = t.quantity,                     productreview = p.rating1                 };      return query; } 

the error throwing thisenter image description here

please try below one, use namespace different context model's safer side.

namespace <newnamespacefordto> //ws.models  {     public class samplemodeldto     {                     public int id { set; get; }             public string productname { get; set; }           public int productquantity { get; set; }            public string productreview { get; set; }         } }  public list<samplemodeldto> getproducts() {      var query = t in db.products.asenumerable()                  join p in db.ratings.asenumerable() on t.product_id equals p.prod_id                  select new samplemodeldto()                  {                       productname=t.product_name,                       productquantity=t.quantity,                       productreview=p.rating1                  };       return query.tolist(); } 

it might useful.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -