.net linq itemcollection

.net - Expresión LINQ básica para un ItemCollection



(2)

Es porque ItemCollection solo implementa IEnumerable , no IEnumerable<T> .

Debe llamar efectivamente a Cast<T>() que es lo que sucede si especifica el tipo de la variable de rango explícitamente:

var lItem = from object item in lListBox.Items where String.Compare(item.ToString(), "abc") == 0 select item;

En notación de puntos, esto es:

var lItem = lListBox.Items .Cast<object>() .Where(item => String.Compare(item.ToString(), "abc") == 0));

Por supuesto, si tiene una mejor idea de lo que está en la colección, podría especificar un tipo más restrictivo que el object .

Tengo un ItemCollection que me gustaría consultar usando LINQ. Probé el siguiente ejemplo (artificial):

var lItem = from item in lListBox.Items where String.Compare(item.ToString(), "abc") == true select item;

Visual Studio me sigue diciendo Cannot find an implementation of the query pattern for source type ''System.Windows.Controls.ItemCollection''. ''Where'' not found. Consider explicitly specifying the type of the range variable ''item''. Cannot find an implementation of the query pattern for source type ''System.Windows.Controls.ItemCollection''. ''Where'' not found. Consider explicitly specifying the type of the range variable ''item''.

¿Cómo soluciono el problema?


Necesitas especificar el tipo de "item"

var lItem = from object item in lListBox.Items where String.Compare(item.ToString(), "abc") == true select item;