Skip Navigation Links
Electric Harbour
Articles
Danish Alt Codes
Finnish Alt Codes
French Alt Codes
Norwegian Alt Codes
Spanish Alt Codes
Scandanavian Alt Codes
Swedish Alt Codes
Bulk Identity Insert
SQL Server Constraints
Visual Studio Shortcuts
LINQ Cast Operator
LINQ Group By Example
LINQ Not In Example
LINQ Order By Example
Tools
CSV String Generator
About
Curriculum Vitæ
Contact

LINQ Cast Operator - Resolving Visual Studio Error - "Could not find an implementation for the query pattern for source type <some type>. ‘Select’ not found. Consider explicitly specifying the type of range variable x.”

Sometimes when writing LINQ queries I get caught off guard when Visual Studio shows a compile error with a message stating “Could not find an implementation for the query pattern for source type [some type]. ‘Select’ not found. Consider explicitly specifying the type of range variable x.”

This error can happen if you don’t have the line using System.Linq; at the start of your code file, but usually it’s because there is a problem with the implementation of IEnumberable<t> or lack thereof.

I came across this recently when trying to get the header text of the Columns in a DataGridView. Here was the code I was using:

var columnHeaders = (from x in dgv.Columns
                     select x.HeaderText).ToArray();

The problem is that the DataGridView Columns property is of type DataGridViewColumnCollection. To get around the problem LINQ is having with this collection I cast the columns to a DataGridViewColumn object instead. Here is the code that works:

var columnHeaders = (from x in dgv.Columns.Cast<DataGridViewColumn>
                     select x.HeaderText).ToArray();

Using this technique almost always fixes the error for me. I find it’s the most useful application of the LINQ cast operator.