我们如何在asp.net c中的数据表上使用linq得到sum(第1列)-sum(第2列)

时间:2018-03-19 10:37:35

标签: c# sql-server linq datatable ado.net

Dattemp表Dr和Cr中有两列。我需要结果和(dr)-sum(cr)和datdetail表的开放平衡列中的dattemp上的where子句

       for (int i = 0; i < datDetail.Rows.Count; i++)
        {
            datDetail.Rows[i]["Opening Balance"] = Convert.ToDouble(DatTemp.AsEnumerable().Where(r => r.Field<int>("Identity ID") == Convert.ToInt32(datDetail.Rows[i]["Identity ID"]) && (r.Field<DateTime>("Date") <= Convert.ToDateTime(TxtFromDate.Text.Trim()))).Sum(r => r.Field<decimal>("CR")) - DatTemp.AsEnumerable().Sum(r => r.Field<decimal>("DR"))).ToString("00.00");
            datDetail.Rows[i]["Clossing Balance"] = Convert.ToDouble(DatTemp.AsEnumerable().Where(r => r.Field<int>("Identity ID") == Convert.ToInt32(datDetail.Rows[i]["Identity ID"]) && (r.Field<DateTime>("Date") <= Convert.ToDateTime(TxtToDate.Text.Trim()))).Sum(r => r.Field<decimal>("CR")) - DatTemp.AsEnumerable().Sum(r => r.Field<decimal>("DR"))).ToString("00.00");
        }

        GridDetails.DataSource = datDetail;
        GridDetails.DataBind();

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

var openBal = DatTemp.Compute("Sum(OpeningBalance)", "");
var closeBal = DatTemp.Compute("Sum(ClosingBalance)", "");
var difference = Math.Abs(openBal - closeBal);//you may need to cast here