views:

2277

answers:

1

I'm trying to perform a simple LINQ query on the Columns property of a DataTable:

from c in myDataTable.Columns.AsQueryable()
    select c.ColumnName

However, what I get is this:

Could not find an implementation of the query pattern for source type 'System.Linq.IQueryable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'c'.

How can I get the DataColumnCollection to play nice with LINQ?

+15  A: 

How about:

var x = from c in dt.Columns.Cast<DataColumn>()
        select c.ColumnName;
Dave Markle
@Dave: What causes this issue in the first place? How come we have to do the Cast() ?
Ryan Shripat
It's because dt.Columns is an IEnumerable, but not an IEnumerable<DataColumn>. It's just a function of the fact that the class is a bit older and doesn't implement the new generic type. When you Cast<>(), you cast to an IEnumerable<T> where the extension methods are defined.
Dave Markle