从 C# 中的 DataColumn 值获取总和 [英] Get sum from a DataColumn values in C#
本文介绍了从 C# 中的 DataColumn 值获取总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 dataadapter
中有一个表.我想获得它特定列的计数和总和.这怎么可能?
I have a table in a dataadapter
. I want to get the count and sum of a specific column of it. How is that possible?
这是到达列的代码,然后呢?
This is the code for reach to the column, what after that?
DataColumn buy_count = myDataSet.Tables["all_saled"].Columns["how_much_buy"]
DataColumn buy_count = myDataSet.Tables["all_saled"].Columns["how_much_buy"]
我知道我们在 SQL 中有 sum、count、...,但我如何在 C# 中做到这一点?
I know that we have sum, count,... in SQL, but how can I do it in C#?
推荐答案
您可以使用 LINQ to DataSets
You can use LINQ to DataSets
var sales = myDataSet.Tables["all_saled"].AsEnumerable();
var buy_total = sales.Sum(datarow => datarow.Field<int>("how_much_buy"));
附言可能需要引用 System.Data.DataSetExtensions 程序集.
P.S. might need the System.Data.DataSetExtensions assembly referenced.
这篇关于从 C# 中的 DataColumn 值获取总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文