Could not find query template implementation. Error

Considering

var selectedItems = listBoxControl1.SelectedItems; var selectedItemsList = (from i in selectedItems select i).ToList(); 

I get an error

Could not find query template implementation for source type 'DevExpress.XtraEditors.BaseListBoxControl.SelectedItemCollection. "Select" not found. Consider explicitly specifying the type of range variable i.

using system.LINQ Done

I can use foreach, so it must implement IEnumerable . I prefer to use LINQ overeeach to collect each row, if possible.

I want to take the ToString() values ​​for each SelectedItem in a list control and paste them into a List<string> . How can i do this?

+4
source share
3 answers

I can use foreach, so it must implement IEnumerable.

This is actually not the case, but it does not matter here. It implements IEnumerable , but not the IEnumerable<T> that LINQ works with.

What is actually on the list? If this is already a string, you can use:

 var selectedItemsList = selectedItems.Cast<string>().ToList(); 

Or, if these are “any objects” and you want to call ToString , you can use:

 var selectedItemsList = selectedItems.Cast<object>() .Select(x => x.ToString()) .ToList(); 

Note that Cast 's call is why the error message is suggested using an explicitly typed range variable - the query expression starting with from Foo foo in bar will be converted to bar.Cast<Foo>()...

+14
source

For LINQ to work, you need an IEnumerable<T> , a direct IEnumerable is not enough. Try:

 var selectedItems = listboxControl1.SelectedItems.Cast<T> //where T is the actual type of the item 
+7
source

Just try

 var result = listBoxControl1.SelectedItems.Cast<MyItemType>().ToList(); 
+1
source

Source: https://habr.com/ru/post/1402661/


All Articles