类型"DataRow"中不存在适用的方法“选择" [英] No applicable method 'Select' exists in type 'DataRow'
问题描述
我有一个数据表结果,并使用AsEnumerable扩展名将其转换为Enumerable.
I have a data table result and I converted it to Enumerable using AsEnumerable extension.
var dataTableToEnumerable = dataTable.AsEnumerable();
现在我使用Linq Dynamic Extensions对它进行groupBy处理,它工作正常,但是我无法对聚集结果进行Select()
,Sum()
,Min()
等处理.
Now Im doing a groupBy on it using Linq Dynamic Extensions and it works fine but I cant process Select()
,Sum()
,Min()
etc on the aggregrated result.
但是存在Count(),First(),FirstOrDefault()之类的方法.
But methods like Count(),First(),FirstOrDefault() exist.
dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null&&it[\"price\"] != null)")
.GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it").Select("it.Sum(\"price\")").
还参考此问题,我更改了我的查询以下内容:-
Also with reference to this question I changed my query to this:-
(dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null&&it[\"price\"] != null)")
.GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it") as IEnumerable<IGrouping<DynamicClass,DataRow>>).Select("it.Sum(\"price\")").
但是现在显示No Method Select exist on IEnumerable<IGrouping<DynamicClass,DataRow>>
给出错误No applicable method 'Select' exists in type 'DataRow'
,
有人可以帮忙吗?
Its giving the error No applicable method 'Select' exists in type 'DataRow'
,
Can Any one help on this?
#Update1
我已经尝试过第一个答案.它工作正常,但是在groupby之后如何更改多个字段呢?
这是我的查询:-
dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null && it[\"price\"] != null)") .GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it") .Select("new(it.Select(it[\"price\"] as price,it[\"cost\"] as cost)")
I have tried with that first Answer. It's working fine, but how can i change it there is multiple field selection after groupby?
this was my query : -
dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null && it[\"price\"] != null)") .GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it") .Select("new(it.Select(it[\"price\"] as price,it[\"cost\"] as cost)")
结果集正确,但
.AsEnumerable() .Select(pg => (((IEnumerable<object>)pg).Sum(p => (double)p)));
The result set is correct but,
.AsEnumerable() .Select(pg => (((IEnumerable<object>)pg).Sum(p => (double)p)));
这导致错误
无法将类型为'<> f__AnonymousType15`2 [System.Object,System.Object]'的对象转换为类型为'System.IConvertible'.
Unable to cast object of type '<>f__AnonymousType15`2[System.Object,System.Object]' to type 'System.IConvertible'.
推荐答案
您可以使用DataRow
字符串索引器方法将price
列提取为object
,然后使用AsEnumerable
,可以得到IEnumerable<object>
,然后将其转换为适当的类型:
You can extract the price
column using the DataRow
string indexer method as an object
and then convert from Dynamic LINQ to LINQ to Objects using AsEnumerable
, which gets you IEnumerable<object>
which you can then cast to the appropriate type:
var ans = dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null && it[\"price\"] != null)")
.GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it")
.Select("it.Select(it[\"price\"])")
.AsEnumerable()
.Select(pg => (((IEnumerable<object>)pg).Sum(p => (double)p)));
如果您不需要GroupBy
中的任何其他字段,则可以将price
列分组:
If you don't need any other fields from the GroupBy
, you can just group the price
column:
var ans = dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null && it[\"price\"] != null)")
.GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)", "it[\"price\"]")
.AsEnumerable()
.Select(pg => (((IEnumerable<object>)pg).Sum(p => (double)p)));
要处理多个列,必须手动调用扩展方法Sum
,因为扩展方法不适用于dynamic
对象,并且必须手动将lambda强制转换为Func
,因为编译器无法告诉您是否需要dynamic
表达式中的表达式树或函数:
To handle more than one column, you must manually call the extension method Sum
because extension methods don't work with dynamic
objects, and you must manually cast the lambdas to Func
since the compiler can't tell if you need an expression tree or function in a dynamic
expression:
var ans = dataTableToEnumerable.AsQueryable().Where("(it[\"yoko\"] != null && it[\"price\"] != null)")
.GroupBy("new(it[\"proposalid\"] as proposalid,it[\"yoko\"] as yoko)")
.Select("it.Select(new(it[\"price\"] as price, it[\"cost\"] as cost))")
//.AsEnumerable()
//.Select(pg => (((IEnumerable<object>)pg).Sum(p => (double)p)))
.AsEnumerable()
.Select(x => new {
sumPrice = Enumerable.Sum(x, (Func<dynamic, double>)(y => y.price)),
sumCost = Enumerable.Sum(x, (Func<dynamic, double>)(y => y.cost))
})
.ToList();
这篇关于类型"DataRow"中不存在适用的方法“选择"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!