Why is this LINQ query not working?

I was trying to help someone else and wrote this request:

var foundTab = (from tab in tabControl1.TabPages
                where tab.Name == "tabName"
                select tab).First();

      

And they reported that they got this error:

Could not find an implementation of the request pattern for the source type System.Windows.Forms.TabControl.TabPageCollection. "Where" was not found. Consider explicitly specifying the range type for the variable 'tab'.

I checked MSDN and TabPageCollection

implemented IList

, ICollection

, IEnumerable

. So what's going on here? What does this error mean and is there any other way to query the Tab Control property TabPages

?

+2


source to share


4 answers


Try the following:

var tab = (from System.Windows.Forms.TabPage tab in tabControl1.TabPages
           where tab.Name == "tabName"
           select tab).First();

      



This code indicates the type of the range variable.

+8


source


TabPageCollection

implements IEnumerable

, but not IEnumerable<T>

, what LINQ uses. To fix, use the casting method like this:



var foundTab = (from tab in tabControl1.TabPages.Cast<TabPage>()
            where tab.Name == "tabName"
            select tab).First();

      

+4


source


But but but....? can you just link to it directly if you have a name? TabPages ["TABNAME"]

+2


source


Try the following:

var tab = tabControl1.TabPages.FirstOrDefault(t => t.Name == "tabName");

      

Also, make sure you have

using System.Linq;

      

at the top of the file.

Dylan

0


source







All Articles