简体   繁体   中英

The entity or complex type ' ' not be constructed in a LINQ to Entities query

I hope someone can help me because when i edit my code . an exception shows

THIS IS MY CONTROLLER

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

This is the exception:

NotSupportedException was unhandle by user code. The entity or complex type ' ' not be constructed in a LINQ to Entities query. LINQ ASP.NET

At this line:

if (applicantdata.Any())

In Linq-to-Entities you can only project to any existing mapped entity type but you can project to an anonymous type

applicantdata = (from a in applicant
                     select new APPLICANT() { 
                         APPLICANT_ID = a.Id, 
                         APPLICANT_LastName = a.LastName
                     }).AsEnumerable();

In the above code you are trying to project to 'APPLICANT' type so it will not allow you to do so. you can try to do it using anonymous type as :

applicantdata = (from a in applicant
                     select new { 
                         APPLICANT_ID = a.Id, 
                         APPLICANT_LastName = a.LastName
                     }).AsEnumerable();

Hope this will help!

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