1

I need compare if a Radcombobox has ItemElements that matches with my expected string. Here is what I'm trying to do:

foreach (IRadComboBoxItem item in comboBox.ItemElements)
{
    var itemExists = comboBox.ItemElements.FirstOrDefault(items => item.Text.Contains(expectedString));
    if (itemExists == null) continue;
    itemExists.Select();
    return true;
}

However comboBox.Text.Contains(expectedString) is not supported as I'm comparing IRadComboBoxItem with a string. Could you please suggest how to achieve this?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
SKN
  • 520
  • 1
  • 5
  • 20

1 Answers1

2

Use linq method of Any:

return comboBox.ItemElements.Any(item => item.Text.Contains(expectedString));

In your above code you mixed a bit the use of different linq methods

  1. In the FirstOrDefault - it returns the first item in a collection that matches a predicate, otherwise default(T).
  2. Then if it is not null you perform an Select but assign it to nowhere.
  3. You have this code in a foreach loop - but do not use the item nowhere. you don't need the loop because you are trying to use the linq methods (which behind the scenes use the loops themselves)

Following comment what you want is:

var wantedItem = comboBox.ItemElements.FirstOrDefault(item => item.Text.Contains(expectedString));
if(wantedItem != null)
{
    //What you want to do with item
}

Didn't work with RadComboBox myself but by this site maybe:

RadComboBoxItem item = comboBox.FindItemByText(expectedString);

I assume that if it doesn't find it returns null

Graham
  • 7,431
  • 18
  • 59
  • 84
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Thanks for your response. I corrected my code. However your suggestion didn't solve my problem. I have a combobox which is databound. So Text property is not available. item.Text is always holding null though comboBox.ItemElements returns the elements. So I'm unable to compare the strings. Further I'm using Select to select that element from combobox that matches with the expectedString – SKN Aug 26 '16 at 06:54