1

I have a textbox bind to an int. There is a Search button which gives result on the basis of text in textbox. If the text is 12, all results having 12 should appear.

Such being the case for string, I would have used string.contains. But I don't know what to do in case of int.

I am using LINQ to filter out results.

int securityId = Convert.int32(filterColumn.Value, CultureInfo.CurrentCulture);
queryResults=queryResults.Where(generaldata=>generaldata.SecuritiesId.Equals(securityId));  

But this isn't working. I tried following

string securityId = Convert.ToString(filterColumn.Value, CultureInfo.CurrentCulture);                                
queryResults=queryResults.Where(generaldata=>generaldata.SecuritiesId.ToString().Contains(securityId));  

But getting LINQ exception that I can't use ToString in expression.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263

2 Answers2

2

i think you should check for less than coz for eg if you count from 0 to 100 and check if it contains 12 then yes it contains 12 because

12 < 100
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
1

Try using SqlFunctions.StringConvert

string securityId = Convert.ToString(filterColumn.Value, CultureInfo.CurrentCulture);    
queryResults=queryResults
      .Where(gd=>SqlFunctions.StringConvert((double)gd.SecuritiesId).Contains(securityId));

P.S: SqlFunctions.StringConvert has no overload for int and that's why the casting to double has been done.

naveen
  • 53,448
  • 46
  • 161
  • 251
  • What I have to import to include SqlFunctions ? – Nitish Oct 27 '12 at 06:13
  • @naveen : I just found out, I have to make query SQL independent. Thus, I cannot use this approach. Is there some other alternative? – Nitish Oct 27 '12 at 06:30
  • why does int.Equals dont work? for me .Equals did not work on certain LINQ scenarios and == worked perfectly. – naveen Oct 27 '12 at 16:27