Enrique Gil
Enrique Gil

Reputation: 754

The Entity or complex type cannot be constructed in a Linq to Entity Query

Good day to all.

Im Having Entity or complex type cannot be encountered This is the first time i encountered this type of error Someone Help me

    public IEnumerable<APPLICANT> GetApplicant()
    { 
        IEnumerable<APPLICANT> applicantdata = Cache.Get("applicants") as IEnumerable<APPLICANT>;

        IEnumerable<Profile> profiledata = Cache.Get("profiles") as IEnumerable<Profile>;




        if (applicantdata == null)
        {

            var applicantList = (from a in context.Profiles
                                 join app in context.APPLICANTs 
                                 on a.PROFILE_ID equals app.Profile_id
                                where app.APPLICANT_LogicalDelete == false
                                select new APPLICANT()
                                 {
               APPLICANT_LastName = a.Applicant_LASTNAME,
               APPLICANT_FirstName = a.Applicant_FIRSTNAME,
               APPLICANT_MiddleName = a.Applicant_MIDDLENAME,
               APPLICANT_Address = a.Applicant_ADDRESS,
               APPLICANT_City = a.Applicant_CITY,
               APPLICANT_Phone = a.Applicant_PHONE,
                APPLICANT_Email= a.Applicant_EMAIL
           });

            applicantdata = applicantList.Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)).OrderBy(v => v.APPLICANT_ID).ToList();

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

    }

And This is the line where i encounter the error Thanks!

applicantdata = applicantList.Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)).OrderBy(v => v.APPLICANT_ID).ToList();

AND THE ERROR IN THE LINE ABOVE IS

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

Upvotes: 2

Views: 8588

Answers (2)

user743382
user743382

Reputation:

select new APPLICANT()

Entity Framework does not support this. It gets diagnosed when the query is used, not when the query is constructed, which is why the line where you get the exception is confusing.

You can construct any type that isn't also a database entity, including anonymous types, so you can do

select new
{
    a.Applicant_LASTNAME,
    a.Applicant_FIRSTNAME,
    a.Applicant_MIDDLENAME,
    a.Applicant_ADDRESS,
    a.Applicant_CITY,
    a.Applicant_PHONE,
    a.Applicant_EMAIL
}

and you can, if required, put those values back into APPLICANT objects after your query has finished executing.

Or, if appropriate, you can the APPLICANT directly:

select app

I'm not sure why you have Applicant_LASTNAME etc. in both Profile and APPLICANT, and I do not know if the values are the same. If they are not, this last suggestion won't be of use to you.

Upvotes: 2

Htin Aung
Htin Aung

Reputation: 579

If I'm not wrong, you need to convert to Queryable objects.

applicantdata = applicantList.AsQueryable().Where(v => !String.IsNullOrEmpty(v.APPLICANT_LastName)).OrderBy(v => v.APPLICANT_ID).ToList();

Upvotes: 0

Related Questions