0

Another complex one, I have searched everywhere and have only been able to find answers to 'how do i find a string in a combo box'

I want to know the opposite. I have a combo box filled with cities (strings) and I am passed an address string.

I would like to search my address string to see if it contains any one of the cities currently in my combo box.

eg. Combo box contains: London, Paris, Berlin

Addresses passed in:

  • 123, street, town, London, Postcode (True)
  • 123, street, town, Rome, Postcode (False)

I was thinking along the lines of

address.Contains(combobox.Any().ToString());

but as you can probably tell, that doesnt work!

Anya Hope
  • 1,301
  • 1
  • 17
  • 33

4 Answers4

5

If your ComboBox contains all string items then you can use Enumerable.OfType<string> and then apply LINQ operator Any like:

bool ifExist = combobox.Items.OfType<string>()
                         .Any(cbItem => address.Contains(cbItem);

For case insensitive comparison do:

bool ifExist = combobox.Items.OfType<string>()
             .Any(cbItem => address.IndexOf(cbItem, 
                                            StringComparison.InvariantCultureIgnoreCase) 
                                            > -1);
Habib
  • 219,104
  • 29
  • 407
  • 436
  • This compiles, give me a second to test, could be the winner! – Anya Hope Feb 27 '15 at 17:16
  • 1
    Much better example than what was previously suggested in regards to doing a for-loop this is much more efficient in my opinion +1 – MethodMan Feb 27 '15 at 17:19
  • 1
    Fantastic, fixed the issue perfectly. Thanks a lot :) – Anya Hope Feb 27 '15 at 17:22
  • @MethodMan: why should it be more efficient? Doesn't it result in the same internal operations? There must be code to cycle over all items of the combobox, searching for given data. Based on what I found, its better readable, but much slower: See: http://stackoverflow.com/questions/3156059/linq-statement-faster-than-foreach-loop – Tobias Knauss Feb 27 '15 at 17:23
  • 1
    @TobiasKnauss, I agree with you partially. If there is going to be any performance difference it would be negligible. It is no more efficient than a for/foreach loop, but it more readable and easier to maintain. But it will not be *much* slower. – Habib Feb 27 '15 at 17:30
  • @TobiasKnauss I agree partially as well however I am not going to debate style / preference your opinion has been noted... – MethodMan Feb 27 '15 at 17:39
  • `OrdinalIgnoreCase` would be a better choice for all cultures. `InvariantCultureIgnoreCase` may not be a good way in some of them. – john chen Feb 27 '15 at 18:15
2

I would like to search my address string to see if it contains any one of the cities currently in my combo box.

bool addressContainsCity = combobox.Items.OfType<string>.Any(item => address.Contains(item));

OR

bool addressContainsCity = combobox.Items.Cast<string>.Any(item => address.Contains(item));
Ian CT
  • 1,301
  • 2
  • 12
  • 22
  • I haven't voted it yet! Don't understand the downvote either! This looks like a great answer, I have tried implementing it but it's not allowing me to use combobox.items.any. (system.windows.forms.combobox.objectcollection does not contain a definitiion for any) Could this be because i'm using .net 4? and any idea how to fix it? – Anya Hope Feb 27 '15 at 17:14
  • Not my downvote, but I guess `Enumerable.Any` can't be applied with `Items` property. It doesn't implement `IEnumerable` – Habib Feb 27 '15 at 17:14
  • 1
    @chutzzz, your current code in the answer doesn't compile. Modify your answer. – user2711965 Feb 27 '15 at 17:17
2

In case you don't like LINQ:

make a for-loop over all combobox items, and call the address.Contains() on all string values of the combobox items.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
0

Well there could be a couple ways you could do this. This is the way I would approach it:

string[] arg = address.Split(',');
combobox.Items.Contains(arg[3]);

This should work if I understand what you are trying to do, and the City is always in the same position in the string.

Chase Ernst
  • 1,147
  • 1
  • 21
  • 53
  • why would the OP need to use the `Split()` function when linq / lambda can be used quickly to determine it something is contained – MethodMan Feb 27 '15 at 17:18
  • I never said this is the only way to do it. I said this is the way I would do it. Only because I am more comfortable with the `Split()` function than I am with lambdas. – Chase Ernst Feb 27 '15 at 17:19