【发布时间】:2014-01-29 04:35:43
【问题描述】:
我有一个像这样的动态 Linq 查询。我需要将结果集转换为数组。但我无法将 IQueryable 类型转换为数组。有什么建议吗?
我的代码:-
var query = Data.AsEnumerable()
.AsQueryable()
.Select("new(it[\"Country\"] as Country)", "it")
.Take(10);
foreach (string str in query) // **getting error Unable to cast object of type 'DynamicClass1' to type 'System.String**
{
}
我这样修复它:- foreach(var str in query) 。
但我现在有另一个问题。我在查询中添加了 where 条件。现在我收到错误“类型'DataRow'中不存在属性或字段'Country'”。 以下是我的查询
var query= Data.AsEnumerable().AsQueryable().Where("Country = @0", "London").Select("new (Country as Country)");
【问题讨论】:
-
查询应该返回什么?解释
string str的用途。 -
AsEnumerable().AsQueryable()似乎是个坏主意... -
感谢您的回复。我使用以下方法修复了它:- foreach(var str in query) 。但我现在有另一个问题。我在查询中添加了 where 条件。现在我收到错误“'DataRow' 类型中不存在属性或字段 'Country'”。以下是我的查询 var query= Data.AsEnumerable().AsQueryable().Where("Country = @0", "London").Select("new (Country as Country)");
-
请用您尝试过的最新代码更新您的问题并说明问题所在。
标签: linq iqueryable dynamic-linq