There are no properties or fields of type 'datarow' in Dynamic Linq.

dynamic-linq

Question

I am using Northwind Customers Table where I fill the dataset and get the datatable.

I am trying to use dynamic linq and want to select columnName dynamically

var qry = MyDataTable.AsEnumerable().AsQueryable().Select("new(Country)");

Right now I have hard coded country but even then I get this error

No property or field 'Country' exists in type 'datarow'

I would like to eventually change this query to take the column name dynamically.

Please help!!! thanks.

1
0
4/13/2011 3:58:20 PM

Popular Answer

The important hint is here (in bold):

No property or field 'Country' exists in type 'datarow'

The extension method AsEnumerable of the DataTable class returns an IEnumerable<T> where T has the type DataRow. Now the Select method of Dynamic LINQ wants to work with this type DataRow which hasn't a property Country of course.

You could try this instead:

var qry = MyDataTable.AsEnumerable().AsQueryable()
    .Select("new(it[\"Country\"] as CountryAlias)");

it now represents a variable of type DataRow and you can use methods of this type and perhaps also the indexer in my example above. (Dynamic LINQ supports accessing array elements by an integer index, but I am not sure though if accessing an indexer with a string key will work.)

4
4/13/2011 4:56:13 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow