How to calculate the sum of the datatable column in asp.net?

后端 未结 9 1169
悲哀的现实
悲哀的现实 2020-11-27 04:15

I have a DataTable which has 5 columns:

  • ID
  • Name
  • Account Number
  • Branch
  • Amount

The DataTable contains 5 rows.

9条回答
  •  感情败类
    2020-11-27 04:27

     this.LabelControl.Text = datatable.AsEnumerable()
        .Sum(x => x.Field("Amount"))
        .ToString();
    

    If you want to filter the results:

     this.LabelControl.Text = datatable.AsEnumerable()
        .Where(y => y.Field("SomeCol") != "foo")
        .Sum(x => x.Field("MyColumn") )
        .ToString();
    

提交回复
热议问题