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

dynamic-linq

Question

In order to fill up the dataset and get the datatable, I use the Northwind Customers Table.

To choose columnName dynamically, I'm attempting to utilize dynamic linq.

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

Even though I now have my country hard coded, I still have this error

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

In the future, I want to modify this query to dynamically take the column name.

Please assist me! thanks.

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

Popular Answer

Here's the key clue, highlighted in bold:

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

Extension techniquesAsEnumerable of theDataTable class yields anIEnumerable<T> when type T is presentDataRow Now thatSelect This type technique of Dynamic LINQ wishes to operate with it.DataRow that lacks a propertyCountry sure enough.

Instead, you may try this:

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

it currently represents a type variable.DataRow and you may use techniques of this kind as well as, perhaps, the indexer from my previous example. (I'm not sure whether accessing an indexer with a string key would work, although Dynamic LINQ enables accessing array items by an integer index.)

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