0

this is my query:

var caly = from m in stan.magazyn
           from g in stan.gdzie
           from p in stan.pracownik
           where m.Gdzie_Jest == g.ID_Miasta
           where m.Kto_Wprowadzil == p.ID_Pracownika
           where m.Gdzie_Jest == combobox_miasto
           orderby m.Marka, m.Model, m.IMEI
           where (m.ID.Contains(textBox13.Text)) 
                  ||  (m.Marka.Contains(textBox13.Text)) 
                  || (m.Model.Contains(textBox13.Text)) 
                  || (m.IMEI.Contains(textBox13.Text)) 
                  || (m.Kolor.Contains(textBox13.Text)) 
                  || (m.Od_Kogo.Contains(textBox13.Text)) 
                  || (m.Info.Contains(textBox13.Text))
          select new { m.ID, m.Marka, m.Model, m.IMEI, m.Kolor, m.Od_Kogo, Sklep = g.Nazwa, m.Data_Wprowadzenia, m.Cena_Kupna, Dodał = p.login, m.Info };

if I can do this: (m.Marka.Contains(textBox13.Text)) and next, it's ok, because in my base this is string. I have problem when I want search ID (m.ID.Contains(textBox13.Text)) because this is integer. what I must do in this situation?

Jamiec
  • 133,658
  • 13
  • 134
  • 193
SSJ
  • 25
  • 5
  • Explain _"search ID"_. If the user enters "1", do you want to find the record with ID 1, or also 10, 11, 12, and so on? – CodeCaster Sep 16 '15 at 12:25
  • If I put "1" I want see ID 1, 11, 15, 21,31,5551,133,412, etc. and other data for example model" nokia 3310". – SSJ Sep 16 '15 at 12:47
  • if I put "3310" I want see for example id 3310, model 3310, and maybe IMEI 33104234823942, and 555331055555 – SSJ Sep 16 '15 at 12:48

3 Answers3

2

You need to convert the value of textBox13.Text to a string. Ideally you would use int.TryParse:

int value = 0;
if(!int.TryParse(textBox13.Text,out value))
   throw new InvalidOperationException("textBox13 must contain a number");

and then

(m.ID.Contains(value))

(Side note: give your textboxes meaningful names - textBox13 means nothing!)


Edit: Thanks to @CodeCaster who has pointed out an almost certain issue with the above - that your text box could be used to search string fields or numeric fields - in which case you probably dont want to throw an exception if parsing the textbox to an integer fails.

If this is the case you probably want two things

  1. A boolean indicating whether you actually have a number
  2. The string value parsed to a number

For example

int value = 0;
bool isNumericalSearch = int.TryParse(textBox13.Text,out value));

These values can then be used as part of your search query:

where (isNumericalSearch && m.ID == value) 
              || ((m.Marka.Contains(textBox13.Text)) 
              || (m.Model.Contains(textBox13.Text)) 
              || (m.IMEI.Contains(textBox13.Text)) 
              || (m.Kolor.Contains(textBox13.Text)) 
              || (m.Od_Kogo.Contains(textBox13.Text)) 
              || (m.Info.Contains(textBox13.Text)))
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • OP has a free search textbox that should be used on all fields. So if the user enters "fooproduct", you don't want to throw. – CodeCaster Sep 16 '15 at 12:26
  • 1
    @CodeCaster I get your point, but are you just assuming that or is it specified anywhere? – Jamiec Sep 16 '15 at 12:28
  • 1
    If you look at OP's code, you see a bunch of ors (`||`). For example there's also an IMEI and Color field. When searching for "red", you don't want to throw. But of course, OP did not specify this. It's not that good a question. – CodeCaster Sep 16 '15 at 12:32
  • Thanks for following my advice. :) – CodeCaster Sep 16 '15 at 12:40
  • CodeCaster, has true. I want search all data in one textbox. – SSJ Sep 16 '15 at 12:44
  • thats great! thansk :) – SSJ Sep 16 '15 at 12:52
  • I think it's better to convert `ID` to string (some kind of inversion of what you do) and use Contains method normally like this `m.ID.ToString().Contains(textBox13.Text)` – Hopeless Sep 16 '15 at 12:53
  • @SSJ you said `If I put "1" I want see ID 1, 11, 15, 21,31,5551,133,412, etc`, so the code here won't do that. It just searches for exactly matched IDs. – Hopeless Sep 16 '15 at 12:54
  • @Jamiec you should read the comment the OP made under his question. – Hopeless Sep 16 '15 at 12:55
  • @Hopeless it was made after I answered this question, in fact i think it was after the OP marked this as their answer. *\*shrug\** – Jamiec Sep 16 '15 at 12:55
  • now I see, that Hopeless had true. Jamiec, dont wokr good :( if I put 316, I see only two imei, but dont see ID. sorry :D – SSJ Sep 16 '15 at 12:57
  • I think you might be looking for @Zeeshan's answer below. – Jamiec Sep 16 '15 at 12:58
  • @SSJ you can try `m.ID.ToString().Contains(textBox13.Text)` instead, at least it should work in EF6, I've not experienced EF5 or earlier versions because I've just started with EF and jumped right to EF6. – Hopeless Sep 16 '15 at 12:59
  • And this is why you should specify the question accurately to start with! – Jamiec Sep 16 '15 at 12:59
  • @SSJ what version of EF are you using? I'm using EF6 and it's great that many not supported exceptions have gone away. If it's an earlier version, so using `SqlFunctions.StringConvert` should work for SQL Server. – Hopeless Sep 16 '15 at 13:08
1

you can use SqlFunction class to convert your int to string.

do somthing like:

(SqlFunctions.StringConvert((double)m.ID)).Contains(textBox13.Text)
Zeeshan
  • 2,884
  • 3
  • 28
  • 47
0

You just need to cast the input value textBox13.Text into an int (let's call it textBox13Integer), then you can use m.ID.Equals(textBox13Integer) or m.ID == textBox13Integer in order to find your result by id.

Slyvain
  • 1,732
  • 3
  • 23
  • 27
Alves RC
  • 1,778
  • 14
  • 26