Dynamic Linq - no property or field exists in type 'datarow'

隐身守侯 提交于 2019-12-01 14:24:18

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.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!