linq 查询 C# 的错误结果

发布于 2024-12-11 08:26:17 字数 876 浏览 0 评论 0原文

我写了这个查询,但结果是错误的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昵称有卵用 2024-12-18 08:26:17

试试这个:

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;

我怀疑三元运算符正在给你带来悲伤。

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.

过期情话 2024-12-18 08:26:17

由于 ?: 表达式,您必须对 boolean 进行分组!请参阅:

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.eUser.englishName.ToLower().Contains(strSearchKey) 之后的所有表达式都将是 AND 和 item.eUser.englishName.ToLower().Contains(strSearchKey) 最终返回一个结果!
ALSO 您可以使用单独的 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;

You must group booleans because of ?: expression! 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!
ALSO you can use separate where clauses:

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;
等往事风中吹 2024-12-18 08:26:17

也许是因为?运算符不在 () 和您正在测试 arabicSymbol 之间吗?

尝试:(为了清晰起见添加了额外的行)

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; 

Perhaps because the ? operator is not between () and you are testing 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; 
眼藏柔 2024-12-18 08:26:17

上面的两个答案都是正确的,因为它们与运算符优先级相关(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文