0

「合計」列を持つデータテーブルがあります。すべての行ではなく、特定の行「合計」を取得できるようにしたい。

public void maxValue()
    {
        string pass = (String)Session["name"];
        DataTable table = (DataTable)Session["CocaCola"];
        int total = table.AsEnumerable().Sum(r => r.Field<int>("Total"));
        int totalAllowed = table.AsEnumerable().Sum(r => r.Field<Int32>("Total Allowed"));

        if (total >= totalAllowed)
        {
            Label1.Text = "Total value exceeded the maximum of " + totalAllowed;
        }
        else if (total < totalAllowed)
        {
            Label1.Text = "Total value which is allowed :" + totalAllowed;
        }
        if (pass.Equals("Low"))
        {
            Label1.Text = "You are not allowed any assets at this Stage";
            //SNS.Checked = false;
            //TT.Checked = false;
            //Music.Checked = false;
            //SNS.Enabled = false;
            //TT.Enabled = false;
            //Music.Enabled = false;
        }
    }

ご覧のとおり、私の方法は機能しますが、実行したくない列を追加します。どうすれば変更できますか?

4

2 に答える 2

0

このようにできます

int yourTargetindex = 0; //Change this to get the value of your target element
int total =(from row in table.AsEnumerable()
            select row.Field<int>("Total")).ElementAt(yourTargetindex);
  //This will return the first value of "total" in the DataTable
于 2012-12-11T12:56:21.527 に答える
0

linq を使用する必要はありませんDataTable には、この種の操作のための組み込みメソッドがあります。

var selectedTotal = table.Compute("sum(Total)", "columnX == 'x'");

Totalこれは、指定された値を持つ行のすべてのセルの合計を計算するようにテーブルに指示しcolumnXます。

もちろんlinqも使えます。Where()合計を計算する前に a を追加する必要があります。

于 2012-12-11T15:11:36.980 に答える