2013-06-14 23 views
6

MVCとの結合クエリに問題があり、その理由がわかりません。エラー:LINQ to Entitiesクエリでエンティティまたは複合型を構築できません

The entity or complex type 'Tusofona_Website.Models.site_noticias' cannot be constructed in a LINQ to Entities query.

マイコントローラー:

private TusofonaDBs db = new TusofonaDBs(); 

    // 
    // GET: /DestaquesMain/ 

    public ActionResult Index() 
    { 
     var query = (from sd in db.site_desquesnoticias 
        join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia 
        where sn.Destaque == 1 
        select new site_noticias { 
         CorpoNoticia = sn.CorpoNoticia, 
         TituloNoticia = sn.TituloNoticia 
        }).ToList(); 

     //return View(db.site_desquesnoticias.ToList()); 
      return View(query); 

    } 

マイモデル:

public class site_destaquesnoticias 
{ 
    [Key] 
    public Int32 IDDestaque { get; set; } 
    public Int32 IDNoticia { get; set; } 
    public string Foto { get; set; } 


} 

public class site_noticias 
{ 
    [Key] 
    public Int32 IDNoticia { get; set; } 
    public string CorpoNoticia { get; set; } 
    public string TituloNoticia { get; set; } 
    public string Foto { get; set; } 
    public Int32 Destaque { get; set; } 
} 

public class TusofonaDBs : DbContext 
{ 
    public DbSet<site_destaquesnoticias> site_desquesnoticias { get; set; } 
    public DbSet<site_noticias> site_noticias { get; set; } 
} 

誰もが私を助けることができますか?

+0

可能な重複[エンティティはエンティティへのLINQで構築することができないのクエリ](http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a- linq-to-entities-query) –

答えて

14

マップされたエンティティには投影できません(this回答を参照)。 )

var query = (from sd in db.site_desquesnoticias 
        join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia 
        where sn.Destaque == 1 
        select new { 
         CorpoNoticia = sn.CorpoNoticia, 
         TituloNoticia = sn.TituloNoticia 
        }).ToList(); 

2直接site_noticiasを選択するために、クエリを反転:よう

1)匿名型を選択の代わりに、実体:

しかし、あなたは物事のカップルを行うことができます。これは、検索するクエリとデータによって異なります。

var query = (from sd in db.site_desquesnoticias 
        join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia 
        where sn.Destaque == 1 
        select sn).ToList(); 

3)あなたがするのを選択するプロパティを投影するいくつかのDTO(データ転送オブジェクト)を使用します。以下は、仕事とあなたが必要なデータを与える場合たとえば、あなたが見ることができ:

public class SiteNoticiasDTO{ 
    public string CorpoNoticia {get;set;} 
    public string TituloNoticia {get;set;} 
    } 

var query = (from sd in db.site_desquesnoticias 
        join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia 
        where sn.Destaque == 1 
        select new SiteNoticiasDTO { 
         CorpoNoticia = sn.CorpoNoticia, 
         TituloNoticia = sn.TituloNoticia 
        }).ToList(); 
+1

2)答えはうまく動作します。おそらく、これはnoobエラーです、私はまだMVCで新しいです:)。どうもありがとうございます。 – macieira

+0

これは動作しますが、結果は編集不可能になります。どのように結果を編集可能に保ちますか? foreach(クエリ内の項目){item.name = "append_something"; }。アイテムを編集してもエラーは返されませんが、機能しません。 – doncadavona

+0

ProductDTOを必要なものにキャストするときに3が機能しませんでした。返された商品エンティティでエラーが返されます。 'xyz.Controllers.ProductDTO'の型を 'xyz.Models.Product'にキャストすることができません。エンティティへのLINQは、EDMプリミティブまたは列挙型のキャストのみをサポートします。私は呼び出し方法にIQueriable を返す必要があります:DTOから必要なエンティティにキャストバックする方法はありますか? – Luke

関連する問題