如何在asp.net中的数据表中找到列的前10行的总和 [英] How do I find the sum of top 10 rows of a column in a datatable in asp .net

查看:75
本文介绍了如何在asp.net中的数据表中找到列的前10行的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的所有





任何人都可以告诉我如何查找一列中前十行的总和数据表在asp .net。



我有一个数据表员工



有文件

Id,PersonName,Amount





我想找到前10行的总金额。我的数据表中有10000行以上。我只想查找前10行的总和



请帮帮我





谢谢...

Hi Dear All


Could any one please tell me how to findout the sum of top 10 rows of a column in a datatable in asp .net.

I have a datatable employee

which has fileds
Id,PersonName, Amount


I would like to find the total Amount of top 10 rows. There are above 10000 rows in my datatable.I just want to findout the sum of first 10 rows

Please help me


Thank You...

推荐答案

这应该做的伎俩:http://msdn.microsoft.com/en-IN/library/system.data.datatable.compute.aspx [ ^ ]



问候..
This should do the Trick : http://msdn.microsoft.com/en-IN/library/system.data.datatable.compute.aspx[^]

Regards..


试试这个.. :)



try this.. :)

  DataTable dtGetadata=GetDataTable();

//call method GetDataTable();










public DataTable GetDataTable ()
       {
            DataTable dtData=new DataTable();
           //Fill datatable with data

           int Total=0;

           for(int i=0;i<10;i++)
           {
               Total+=Convert.ToInt32(dtData.Rows[i]["amount"]);

           }
               dtData.Rows.Add("","Total",Total);

           return dtData;

       }


.net 3.5 +如果你的列类型是int,你可以在LINQ下面尝试列名是Amount



.net 3.5 + you can try below with LINQ, if your column type is int and the column name is Amount

var sum = datatable.AsEnumerable().Take(10)
    .Sum(x => x.Field<int>("Amount" ));</int>


这篇关于如何在asp.net中的数据表中找到列的前10行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆