![](/img/trans.png)
[英]the entity or complex type 'x' cannot be constructed in a linq to entities query
[英]The entity or complex type ' ' not be constructed in a LINQ to Entities query
我希望有人可以帮助我,因为当我编辑代码时。 异常显示
这是我的控制者
public IEnumerable<APPLICANT> GetApplicant()
{
IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>;
if (applicantdata == null)
{
var data = from app in context.APPLICANTs
join a in context.Profiles
on app.Profile_id equals a.PROFILE_ID into output
from j in output.DefaultIfEmpty()
select new {
Id = app.APPLICANT_ID,
LastName =
(j == null ? app.APPLICANT_LastName : j.Applicant_LASTNAME)
};
var applicant = data
.Where(v => !String.IsNullOrEmpty(v.LastName))
.Take(1000);
applicantdata = (from a in applicant
select new APPLICANT() {
APPLICANT_ID = a.Id,
APPLICANT_LastName = a.LastName
}).AsEnumerable();
if (applicantdata.Any())
{
Cache.Set("applicants", applicantdata, 30);
}
}
return applicantdata;
}
这是例外:
用户代码未处理NotSupportedException。 实体或复杂类型''不能在LINQ to Entities查询中构造。 LINQ ASP.NET
在这一行:
if (applicantdata.Any())
在Linq-to-Entities中,您只能投影到任何现有的映射实体类型,但可以投影到匿名类型
applicantdata = (from a in applicant
select new APPLICANT() {
APPLICANT_ID = a.Id,
APPLICANT_LastName = a.LastName
}).AsEnumerable();
在上面的代码中,您尝试将项目投影为“ APPLICANT”类型,因此将不允许您这样做。 您可以尝试使用匿名类型来做到这一点:
applicantdata = (from a in applicant
select new {
APPLICANT_ID = a.Id,
APPLICANT_LastName = a.LastName
}).AsEnumerable();
希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.