简体   繁体   中英

LINQ issue with Equals statement

I'm getting an error for the following:

var answerList = (from answer in db.QuestionAnswers
                 where answer.tLoginID.Equals(tId) && answer.pLoginID.Equals(pId)
                    && answer.Submitted.Equals(submittedVal)
                select answer).ToList();

The error is:

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

However, when I change it to:

var answerList = (from answer in db.QuestionAnswers
                 where answer.tLoginID.Equals(tId) && answer.pLoginID.Equals(pId)
                select answer).ToList();

Then I do this:

            answerList.Where(x => x.Submitted.Equals(submittedVal));

It works... What am I missing? To me these statements are doing the same thing. But I'm not sure why it is working like this.

UPDATE:

I figured out after looking at the link that @SergeyLitvinov provided about .Equals that the column I was checking Submitted was actually a Boolean instead of an Integer once I made the types the same my statements worked. Although I did change it from .Equals to == .

The error is from the LINQ to Entities query provider which is attempting to transform your query expression into a SQL statement. Your second example enumerates your LINQ to Entities query prior to testing for Submitted.Equals(submittedVal) , which mean you're using standard LINQ to Objects in local memory (ie it is not converted to SQL).

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