webdad3
webdad3

Reputation: 9080

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 ==.

Upvotes: 0

Views: 214

Answers (1)

Moho
Moho

Reputation: 16498

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 (i.e. it is not converted to SQL).

Upvotes: 2

Related Questions