0

目前正在修复我的代码,直到发生此异常

System.NotSupportedException: The entity or complex type Model.APPLICANT' cannot 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 applicantList = (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 APPLICANT() { APPLICANT_ID = app.APPLICANT_ID, APPLICANT_LastName = (j == null ? app.APPLICANT_LastName : j.Applicant_LASTNAME) }).Take(1000).AsEnumerable().AsQueryable();

        applicantdata = applicantList.Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)).AsEnumerable();



        if (applicantdata.Any())
        {
            Cache.Set("applicants", applicantdata, 30);
        }
    }
    return applicantdata;

}

异常出现在这一行

if (applicantdata.Any())

我希望有人可以提出建议或找到解决此问题的方法。. 谢谢

4

1 回答 1

1

由于您无法在查询中创建非 EF 类型的新实例,因此您可以将查询拆分为两部分。

首先你得到数据

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 applicantData = data.Take(1000)
  .Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName));

然后初始化实例

var applicants = (from a in applicantData
                  select new APPLICANT() { 
                    APPLICANT_ID = a.Id, 
                    APPLICANT_LastName = a.LastName
                  }
                 ).AsEnumerable();
于 2013-05-21T09:21:04.487 回答