linq 查询 C# 的错误结果
我写了这个查询,但结果是错误的
var query = from item in db.Advances
where CurrentConfiguration.currentLanguage == GeneralDefinitions.arabicSymbol
? item.eUser.name.ToLower().Contains(strSearchKey)
: item.eUser.englishName.ToLower().Contains(strSearchKey)
&& !item.isPaid
&& item.expectedPaymentMonth == dExpectedPayment.Month
&& item.expectedPaymentYear == dExpectedPayment.Year
&& item.advanceTypeId == (int)enumAdvanceType.AtOnce
select item;
错误在于
item.expectedPaymentMonth == dExpectedPayment.Month
&& item.expectedPaymentYear == dExpectedPayment.Year
尽管 item.expectedPaymentMonth != dExpectedPayment.Month
但它总是 true
此查询中是否有任何语法错误或错误?
I write this query but the result from it is wrong
var query = from item in db.Advances
where CurrentConfiguration.currentLanguage == GeneralDefinitions.arabicSymbol
? item.eUser.name.ToLower().Contains(strSearchKey)
: item.eUser.englishName.ToLower().Contains(strSearchKey)
&& !item.isPaid
&& item.expectedPaymentMonth == dExpectedPayment.Month
&& item.expectedPaymentYear == dExpectedPayment.Year
&& item.advanceTypeId == (int)enumAdvanceType.AtOnce
select item;
The wrong is in
item.expectedPaymentMonth == dExpectedPayment.Month
&& item.expectedPaymentYear == dExpectedPayment.Year
It is always true
although item.expectedPaymentMonth != dExpectedPayment.Month
Is there any syntax error or something wrong in this query ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
我怀疑三元运算符正在给你带来悲伤。
Try this:
I suspect that the ternary operator is causing you grief.
由于
?:
表达式,您必须对boolean
进行分组!请参阅:如果不使用
()
,则item.eUser.englishName.ToLower().Contains(strSearchKey)
之后的所有表达式都将是AND 和
item.eUser.englishName.ToLower().Contains(strSearchKey)
最终返回一个结果!ALSO 您可以使用单独的 where 子句:
You must group
boolean
s because of?:
expression! See:If you don't use
()
, all expressions afteritem.eUser.englishName.ToLower().Contains(strSearchKey)
will beAND
withitem.eUser.englishName.ToLower().Contains(strSearchKey)
and finally returns a single result!ALSO you can use separate where clauses:
也许是因为?运算符不在 () 和您正在测试 arabicSymbol 之间吗?
尝试:(为了清晰起见添加了额外的行)
Perhaps because the ? operator is not between () and you are testing arabicSymbol ?
try: (extra lines added for clarity)
上面的两个答案都是正确的,因为它们与运算符优先级相关(http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx
)在 ?: 之前评估。因此,您实际上可以看到所有 && 都应用于 ?: 表达式的 else 部分。
Both answers above are correct as they relate to operator prescedence (http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx)
The && is evaluated before the ?:. Therefore you're effectively seeing all the &&'s being applied the the else portion of the ?: expression.