简体   繁体   中英

System.NotSupportedException:The entity or complex type Model.APPLICANT cannot be constructed in a LINQ to Entities query

Currently fixing my Codes until this exception occured

System.NotSupportedException: The entity or complex type Model.APPLICANT' cannot be constructed in a LINQ to Entities query

This is my Controller :

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;

}

The exception appears at this line

if (applicantdata.Any())

I hope someone can suggest or can find a way to solve this problem . . thanks

Since you can't create new instances of a non-EF type in query you can split the query into two parts.

First you get the data

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

Then you initialize the instances

var applicants = (from a in applicantData
                  select new APPLICANT() { 
                    APPLICANT_ID = a.Id, 
                    APPLICANT_LastName = a.LastName
                  }
                 ).AsEnumerable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM