Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi everyone

I have an issue in the following Linq to collection query it give me the wrong result and i spend a lot of hours to discover the issue, so kindly tell me if you see anything error:

The wrong is on the condition

The case is:
I want to filter the listed items be provide a search criteria for the user
so the user will select the Item_Class and enter the available amounts then choose the mathematical sign (<, <=, >=, >, ==) so the listed items will match the search criteria

The results totally ignore the final condition i don't know why ?

from item in ItemsList where

!String.IsNullOrEmpty(txtBoxItemCode.Text)? (item.Code.ToString() == (txtBoxItemCode.Text)) : true &&

!String.IsNullOrEmpty(txtBoxItemName.Text)? (item.ItemName.Contains(txtBoxItemName.Text)) : true &&

(DDLClass.SelectedIndex != 0)? item.Class.Equals(DDLClass.SelectedItem as ItemsClass) : true &&

!String.IsNullOrEmpty(txtBoxQuantity.Text)? Compute(item.Quantity, float.Parse(txtBoxQuantity.Text), DDLQuantitySign.SelectedValue.ToString()) : true

select item;

public static bool Compute(float param1, float param2, string op)
    switch (op)
        case "<":
            return param1 < param2;

        case "<=":
            return param1 <= param2;
        case ">":
            return param1 > param2;

        case ">=":
            return param1 >= param2;

        case "=":
            return param1 == param2;

            return false;


Thanks in advance :)
Updated 11-Jun-13 19:15pm
Mehdi Gholam 12-Jun-13 1:09am
"wrong results" is not a good definition and you should not expect help without providing more information.
AhmedYehiaK 12-Jun-13 1:16am
Improved ,,, Thanks :)
Andy Lanng 12-Jun-13 8:16am
I don't get it. Switch statements require a break; for each case. I don't see how that compiles. Are you sure it does?
AhmedYehiaK 12-Jun-13 8:24am
Yeah i am sure; Switch statements don't require a break in case of RETURN statement if you add the break; statement after return statement you will get a warning say that the break; statement is UNREACHABLE statement
Andy Lanng 12-Jun-13 8:28am
Ah right. k
johannesnestler 13-Jun-13 5:54am
Did you find the error in between? Just a small hint: don't do all the parsing inside the expression (you know the query may executed much later (when enumerating results), before that you are just building the "Expression tree"!!! And of course this is considered very bad practice (there are others: you don't check if parsing will work,-> hard debugging, hard mocking for tests, and in my opinion the code reading user input and the code working with it, should always go in these steps: 1. read Input - 2. VALIDATE Input 3. handle possible errors 4. handle input (compute) 5. present result (or error if computation had an error) - so your query should just take all needed info as "Parameters" and then your code would become easy - no checking for null strings during computation Phase, no parsing ... - if you read and validated the user input upfront) - Maybe this would even help you to spot your bug...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900