views:

105

answers:

1

I'm creating some extension methods and I'm getting some errors with RadComboBoxItemCollection, RadComboBoxItemCollection appears to implement IEnumerable but linq keeps giving me errors saying:

"Could not find an implementation of the query pattern for source type 'Telerik.Web.UI.RadComboBoxItemCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 'myItem'."

from this code

public static bool ContainsValue(this RadComboBoxItemCollection myList, string value)
{
      bool matches = (from myItem in myList where myItem.Value == value select myItem).Count() > 0;
      return matches;
}

on the flip side RadListBoxItemCollection works just fine

public static bool ContainsValue(this IEnumerable<RadListBoxItem> myList, string value)
{
      bool matches = (from myItem in myList where myItem.Value == value select myItem).Count() > 0;
      return matches;
}

I tried doing IEnumerable and this solves the linq errors but I get this error

"Instance argument: cannot convert from 'Telerik.Web.UI.RadComboBoxItemCollection' to 'System.Collections.Generic.IEnumerable'"

+1  A: 

The RadComboBoxItemCollection implements the non-generic IEnumerable interface (rather than doing the sensible thing and implementing IEnumerable<RadComboBoxItem>), so your standard LINQ operations won't work. You would have to use the "Cast" extension method first:

 var result = myList.Items.Cast<RadComboBoxItem>();

Now you have a much more useful IEnumerable<RadComboBoxItem> that you can do all sorts of wonderful things with:

public static bool ContainsValue(this RadComboBoxItemCollection myList, string value)
{
      return myList.Items.Cast<RadComboBoxItem>().Count(item => item.Value.Equals(value, StringComparison.Ordinal)) > 0;
}

However, someone with more experience than me could probably speak to the performance of this approach; it might be better for performance to just do it the old (pre-LINQ) way rather than casting each object to a RadComboBoxItem:

public static bool ContainsValue(this RadComboBoxItemCollection myList, string value)
{
      foreach (var item in myList)
          if (item.Value.Equals(value, StringComparison.Ordinal))
              return true;

      return false
}
DivisionByZorro
you where right, I used a similar fix using a for loop, Telerik is updating there controls to fix this issue
Bob The Janitor