Click here to Skip to main content
15,881,852 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 ?


C#
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;



C#
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;

        default:
            return false;

    }
}



Thanks in advance :)
Posted
Updated 11-Jun-13 19:15pm
v2
Comments
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
#^_^#

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