简体   繁体   English

LINQ问题与Equals语句

[英]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'. 无法创建“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. 我在看了@SergeyLitvinov提供的关于.Equals的链接之后想出来,我检查的列Submitted实际上是一个Boolean而不是Integer一旦我使类型与我的语句相同。 Although I did change it from .Equals to == . 虽然我确实将它从.Equals更改为==

The error is from the LINQ to Entities query provider which is attempting to transform your query expression into a SQL statement. 错误来自LINQ to Entities查询提供程序,它正在尝试将查询表达式转换为SQL语句。 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). 您的第二个示例在测试Submitted.Equals(submittedVal)之前枚举您的LINQ to Entities查询,这意味着您在本地内存中使用标准LINQ to Objects(即它未转换为SQL)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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