的DataRow []聚合函数C# [英] DataRow[] Aggregate Functions C#
问题描述
我有一个DataRow对象的数组在我的C#项目,我想总结各种字段。
我注意到了DataRow []。Sum<>函数,而不是遍历每一行,总计我自己的总和,但我很难找到网上的任何资源使用它。
任何指向正确方向的指针都会非常有用
:)删除了代码示例,因为它是错误的!现在工作正常 - 链接帮助马克。
这是LINQ c $ c>;可以使用:
var sum = rows.Sum(row => row.Field< int>(SomeColumn )); //使用正确的数据类型
(你也可以传入列索引或
$ b
var sum = rows.Sum(row => row.SomeColumn);
键入数据行。
MSDN 有关于此的完整文档;例如,这里是我在上面的示例中使用的重载。
I have an array of DataRow objects in my C# project which I would like to Sum various fields from.
Instead of running through each row and totalling my own sums I noticed the DataRow[].Sum<> function but I am struggling to find any resources on the net on how to use it.
Any pointers in the right direction would be very helpful
:) Removed code example as it was wrong! All now works fine - the link helped cheers Marc.
That is the LINQ Sum
; and can be used:
var sum = rows.Sum(row => row.Field<int>("SomeColumn")); // use correct data type
(you can also pass in the column index or the DataColumn
in place of the string)
for untyped datarows, or:
var sum = rows.Sum(row => row.SomeColumn);
for typed datarows.
MSDN has full documentation on this; for example, here is the overload I am using in the examples above.
这篇关于的DataRow []聚合函数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!