0

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 ?

3
  • 1
    Do you get the same result if you replaced the && with separate where clauses? Commented Oct 22, 2011 at 11:30
  • @Enigmativity it is working perfect you can edit it and put it as answer to mark it as a right answer Thanks Commented Oct 22, 2011 at 11:35
  • all answers are solving the problem thanks all Commented Oct 22, 2011 at 11:40

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;

Comments

1

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; 

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.