-2

In my case the Filter is an int value. I want the user to be able to enter the values like this (1,2,3,4,5,..). I applied this query with an string value and it working just fine

if (Country != null)
{
     String[] Country_Array = Country.Split(new Char[] { ',' });
     query = from p in query where Country_Array.Contains(p.Hotel_Country) select p;
}

Now I tried to do the same query to an Int filter but its not working:

if (Hotel_Number != null)
{
     String [] Hotel_Array = (Hotel_Number.ToString().Split(new Char[] { ',' }))   ;
     query = from p in query where Hotel_Array.Contains(p.Customer_No_) select p; 
     //Hotel_Array.Contains(p.Costomer_No_) select p; 
}

I get an Error at the p.cutomer_no! I also tried to convert it to:

p.Customernumber.toString().toArrayChar()

But its also not working.

any idea how to solve it ?

Liam
  • 27,717
  • 28
  • 128
  • 190
Dragon
  • 85
  • 9
  • You need to parse the string values to int. – juharr Mar 23 '15 at 11:21
  • 1
    Also, what is `Hotel_Number`? – Sayse Mar 23 '15 at 11:33
  • Dear All, after many attempts I was not able to apply this method with int values. although i pares it and I converted it I was not able to do it. But I had another idea which is: I will go to my Database column and I will change the column type from int to String and this for sure will work. I already tried it and its working fine. – Dragon Mar 24 '15 at 15:27

2 Answers2

1

The problem is that you are comparing an integer value to an array of strings. You need to parse the values for this to work.

var Hotel_Array = Hotel_Number.Split(new []{','}, StringSplitOptions.RemoveEmptyEntries)
                              .Select(s => int.Parse(s))
                              .ToArray();
juharr
  • 31,741
  • 4
  • 58
  • 93
  • Hi , I am getting an error when I enter the values in the filter text box. Error= "Value enterd('1,2') is not valid, revert to pervious vale. Conversation Error: Input String was not in a correct format"..... Ps: Hotel_Number is an int ( I canot say int.split) – Dragon Mar 23 '15 at 12:28
  • @Zayed That doesn't look like a parsing error. Do you have a stack trace you could share? – juharr Mar 23 '15 at 12:33
  • I am new to C#, and I am doing this in light switch 2011. I just wanted to expand my filter function. Ex. If i want to search for more than one customer I want to write 1,2,2,5451, and so on. – Dragon Mar 23 '15 at 12:38
  • @Zayed unfortunately what you've given me isn't enough to determine what the issue is. I did however update my answer to include `SringSplitOptions.RemoveEmptyEntries` so that trailing commas will not cause the parsing to fail on a blank string. – juharr Mar 23 '15 at 12:44
  • Sorry I did what you say, But I am getting the same error :( var Hotel_Array = Hotel_Number.ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => int.Parse(s)) .ToArray(); – Dragon Mar 23 '15 at 12:50
  • I guess the problem here is that the field is from Type int. and maybe the conversion try to insert it as a string. ? – Dragon Mar 23 '15 at 12:56
  • @Zayed If the value in `Hotel_Number` has anything other than numeric digits, spaces, or commas then it will fail to parse (or if there is a space between numeric digits) . Beyond that I cannot say what your problem might be. – juharr Mar 23 '15 at 13:51
1

I'm guessing query.Customer_No_ is an int. You can't test that against a string without parsing.

int[] numbers = Hotel_Number.ToString()
                            .Split(',')
                            .Select(i => int.Parse(i))
                            .ToArray();

query = from p in query where numbers.Contains(p.Customer_No_)
        select p; 
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Hi , I tried your solution. but iam getting an error when I enter the values in the filter text box. Error= "Value enterd('1,2') is not valid, revert to pervious vale. Conversation Error: Input String was not in a correct format" – Dragon Mar 23 '15 at 12:21