简体   繁体   English

LINQ查询C#错误的结果

[英]wrong result from linq query c#

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 ? 尽管item.expectedPaymentMonth != dExpectedPayment.Month始终是true但此查询中是否存在语法错误或某些错误?

You must group boolean s because of ?: expression! 您必须因为?:表达式而将boolean s分组! See: 看到:

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;

If you don't use () , all expressions after item.eUser.englishName.ToLower().Contains(strSearchKey) will be AND with item.eUser.englishName.ToLower().Contains(strSearchKey) and finally returns a single result! 如果你不使用()所有的表情后item.eUser.englishName.ToLower().Contains(strSearchKey)ANDitem.eUser.englishName.ToLower().Contains(strSearchKey)最后返回一个结果! ALSO you can use separate where clauses: 可以使用单独的where子句:

var query = from item in db.Advances
            where 
                CurrentConfiguration.currentLanguage == GeneralDefinitions.arabicSymbol 
                ? item.eUser.name.ToLower().Contains(strSearchKey)
                : item.eUser.englishName.ToLower().Contains(strSearchKey)
            where !item.isPaid 
                && item.expectedPaymentMonth == dExpectedPayment.Month 
                && item.expectedPaymentYear == dExpectedPayment.Year
                && item.advanceTypeId == (int)enumAdvanceType.AtOnce
            select item;

Try this: 尝试这个:

var query =
    from item in db.Advances
    where (CurrentConfiguration.currentLanguage
            == GeneralDefinitions.arabicSymbol
            ? item.eUser.name
            : item.eUser.englishName).ToLower().Contains(strSearchKey)
    where !item.isPaid
    where item.expectedPaymentMonth == dExpectedPayment.Month
    where item.expectedPaymentYear == dExpectedPayment.Year
    where item.advanceTypeId == (int)enumAdvanceType.AtOnce
    select item;

I suspect that the ternary operator is causing you grief. 我怀疑三元运算符使您感到悲伤。

Perhaps because the ? 也许是因为? operator is not between () and you are testing arabicSymbol ? 运算符不在()之间,并且您正在测试arabicSymbol吗?

try: (extra lines added for clarity) 尝试:(为清楚起见添加了额外的行)

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; 

Both answers above are correct as they relate to operator prescedence (http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx) 上面的两个答案都是正确的,因为它们与运算符的优先级有关(http://msdn.microsoft.com/zh-cn/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. 因此,您可以有效地看到所有&&在?:表达式的else部分得到了应用。

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

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