tags:

views:

112

answers:

3

I was attempting to help someone else out and wrote this query:

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

And they reported that they received this error:

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

I checked MSDN and TabPageCollection implements IList, ICollection, IEnumerable. So, what's going on here? What does that error mean and is there another way to query the Tab Control's TabPages property?

+4  A: 

Try this:

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

This code specifies the type of the range variable.

Robert Harvey
I always forget that one :)
leppie
Looks a little cleaner than the cast method. Thanks!
jasonh
A: 

Try this:

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

Also, make sure you have

using System.Linq;

at the top of your file.

Dylan

Dylan Vester
+3  A: 

TabPageCollection implements IEnumerable but not IEnumerable<T> which is what LINQ uses. To fix, use the cast method like so:

var foundTab = (from tab in tabControl1.TabPages.Cast<TabPage>()
            where tab.Name == "tabName"
            select tab).First();
Cameron MacFarland
Besides the original mistakes I made ("tab" twice and I forgot the select clause, I'm surprised no one noticed, LOL!), this works perfectly. Thanks for mentioning the `IEnumerable`/`IEnumerable<T>` explanation.
jasonh
Error : Aquery body must end with a select or group clause
Anuya
Huh! Copy-n-paste error :P
Cameron MacFarland