简体   繁体   中英

Get sum from a DataColumn values in C#

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"]

I know that we have sum, count,... in SQL, but how can I do it in C#?

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")); 

Check the LINQ to DataSets 101 Samples

PS might need the System.Data.DataSetExtensions assembly referenced.

使用DataTable.Compute方法:

int total = (int)myDataSet.Tables["all_saled"].Compute("SUM(how_much_buy)", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM