Asp.net mvc 错误:无法在LINQ to Entities查询中构造实体或复杂类型

Asp.net mvc 错误:无法在LINQ to Entities查询中构造实体或复杂类型,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我对MVC的连接查询有问题,我不知道为什么 无法在LINQ to Entities查询中构造实体或复杂类型“Tusofona_Website.Models.site_noticias” 我的控制器: private TusofonaDBs db = new TusofonaDBs(); // // GET: /DestaquesMain/ public ActionResult Index() { var query = (from s

我对MVC的连接查询有问题,我不知道为什么

无法在LINQ to Entities查询中构造实体或复杂类型“Tusofona_Website.Models.site_noticias”

我的控制器:

    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; }
}
public类站点\u destaques通知
{
[关键]
public Int32 IDDestaque{get;set;}
公共Int32 IDNOTIIA{get;set;}
公共字符串Foto{get;set;}
}
公共类网站
{
[关键]
公共Int32 IDNOTIIA{get;set;}
公共字符串CorpoNoticia{get;set;}
公共字符串TituloNoticia{get;set;}
公共字符串Foto{get;set;}
public Int32 Destaque{get;set;}
}
公共类TusofonaDBs:DbContext
{
公共数据库集站点_desquesnoticias{get;set;}
公共数据库集站点{get;set;}
}

有人能帮我吗?

你不能投射到映射的实体上(参见答案)

但是,您可以做几件事:

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 new {
                        CorpoNoticia = sn.CorpoNoticia,
                        TituloNoticia = sn.TituloNoticia
                    }).ToList();
2) 反转查询以直接选择站点。这取决于查询和要检索的数据。例如,您可以查看以下各项是否有效,并为您提供所需的数据:

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();

答案很好。这可能是一个noob错误,我对MVC还是个新手:)。非常感谢。这是有效的,但是结果是不可编辑的。如何使结果保持可编辑状态?类似于foreach(查询中的项){item.name=“append\u something”;}。编辑项目不会返回任何错误,但它不起作用。3对我不起作用,因为将ProductDTO强制转换为所需的产品实体会返回错误:无法将类型“xyz.Controllers.ProductDTO”强制转换为类型“xyz.Models.Product”。LINQ to实体仅支持强制转换EDM基元或枚举类型。我需要将iQuery返回到分页方法:是否有一种方法可以从DTO回溯到所需的实体?1。不适合我。同上:强制转换为我得到的所需返回类型:“无法强制转换类型匿名类型”为类型“xyz.Models.Product”。LINQ to Entities仅支持强制转换EDM基元或枚举类型的可能副本