asp.net-mvc – 错误:无法在LINQ to Entities查询中构造实体或
发布时间:2020-12-15 19:00:10 所属栏目:asp.Net 来源:网络整理
导读:我有一个与MVC连接查询的问题,我不知道为什么. 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: /Destaq
我有一个与MVC连接查询的问题,我不知道为什么.
我的控制器: 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; } } 有人可以帮帮我吗? 解决方法
您无法投影到映射的实体(请参阅
this答案).
但是,您可以做以下几件事: 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)反转您的查询以直接选择site_noticias.这取决于您要检索的查询和数据.例如,您可以查看以下内容是否有效并为您提供所需的数据: 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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 合并两个ASP.NET成员资格数据库
- asp.net – 在某些页面上禁用ScriptManager
- asp.net – 是否有可能过滤SignalR中的接收器?
- asp.net-mvc – ASP.net MVC中的第二级视图
- asp.net-core – MVC 6安装为Windows服务(ASP.NET Core 1.0
- 最大请求长度超出例外
- asp.net-mvc – 使用BootstrapValidator与MVC DataAnnotati
- asp.net-mvc – ASP.Net MVC 3中的远程验证:如何在Action方
- ASP.NET 4 URL限制:为什么URL不能包含任何内容?人物
- asp.net-mvc – 加载符号时Visual Studio调试痛苦地慢
推荐文章
站长推荐
- asp.net-mvc – 从基本控制器继承LINQ-to-SQL数据
- asp.net-mvc – 如何组合两个dataTextFields的Se
- .net – 使用PowerPivot的MVC 4 WebApi?
- 使用带有自定义角色提供程序的asp.net mvc 4登录
- asp.net-web-api – 使用Web API的Autofac:从Ht
- ASP.NET Web API为单个路由定制IHttpControllerS
- asp.net-mvc-3 – Elmah没有发送电子邮件
- 动态数据 – ASP.NET动态数据向页面添加其他过滤
- asp.net-mvc – 如何在MVC Razor视图中查找编译时
- asp.net-mvc-4 – 从ASP.NET MVC Action调用Conf
热点阅读