如何计算公式并在数据表中添加行

时间:2017-04-07 22:47:44

标签: c# sql asp.net

我在DaTaTable对象中有来自数据库的数据,我正在基于字符串列表中定义的某个公式执行行计算但不幸的是我最终得到了错误的计算,因为行数值与给定公式不匹配所以计算出错了 对于e.x(管道前收入,扣除/ amort和MSR)=净生产收入 - 个人总成本 - 其他成本+服务费收入

对于循环中的第一个值来自数据表是总个人成本,第二个是其他成本第三个是服务费收入和上一个净生产收入。形成这个公式的 管道收入,减少/分摊和MSR =个人总成本 - 其他成本+服务费收入+净生产收入。

请给我一个解决方案,在下面的代码中,此代码正在执行给定公式的行计算

for (int P = 0; P < dt.Rows.Count; P++)
{                                
        rowName = dt.Rows[P]["GLDescription"].ToString();

        AddColume[0] = "";
        if (AddColume.Contains(rowName))
        {
            int l = 1;

            var DRValue = dt.Rows[P][j];

            if (!(DRValue == null || DRValue == DBNull.Value))
            {

                    if (Expression[index] == "+")
                    {
                        totalRow += Convert.ToDecimal(DRValue);
                    }
                    else if (Expression[index] == "-")
                    {
                        totalRow -= Convert.ToDecimal(DRValue);
                    }

                index++;                                            
            }
        }
        else if (AddColume.Contains(rowName) && AddColume.Contains("self"))
        {
            var DRValue = dt.Rows[P][j];
            if (!(DRValue == null || DRValue == DBNull.Value))
            {
                decimal CurrentValue = Convert.ToDecimal(DRValue);
                dt.Rows[P][j] = CurrentValue * -1;
            }
        }


}

为了你支持我有这个代码

List<string> lstGrouping = new List<string>();
lstGrouping.Add("Volume↔Volume↔Volume↔Sigular");
lstGrouping.Add("Units↔Units↔Units↔Sigular");
lstGrouping.Add("Blank↔Blank↔Sigular");
lstGrouping.Add("Total Production Revenue↔Secondary-Net (Sell - Buy +/- Hedge)↔Branch Brokered Loan Income↔add");
lstGrouping.Add("Total Direct Costs↔Deed Recording↔Provisions for Loan Losses↔add");
lstGrouping.Add("Net Production Income↔Total Production Revenue↔Total Direct Costs↔+$-");
lstGrouping.Add("Total Personnel Costs↔Accounting Dept↔Employer payroll tax exp↔add");
lstGrouping.Add("Total G&A Costs and Other↔Bank Fees↔Provisions for Branch Losses↔add");
lstGrouping.Add("Servicing Fee Income↔Servicing Fee Income , net of expenses↔Servicing Fee Income , net of expenses↔Sigular");
lstGrouping.Add("Income before pipeline, depr/amort and MSR↔Net Production Income↔Total Personnel Costs↔Total G&A Costs and Other↔Servicing Fee Income↔+$+$-$-");
lstGrouping.Add("Other items Total↔Servicing Fee Income , net of expenses↔Depreciation Expense↔add");
lstGrouping.Add("Net Income↔Income before pipeline, depr/amort and MSR↔Other items Total↔+$+");
for (int i = 0; i < lstGrouping.Count; i++)
{

    var AddColume = lstGrouping[i].Split('↔');

    var Expression = AddColume[(AddColume.Length) - 1].Split('$');
    int startindex = 0;
    int Endindex = 0;

    foreach (DataRow DR in dt.Rows)
    {
        string GlDescription = DR["GLDescription"].ToString();
        if (GlDescription.Trim() == AddColume[1].Trim())
            startindex = dt.Rows.IndexOf(DR);
        else if (GlDescription.Trim() == AddColume[2].Trim())
            Endindex = dt.Rows.IndexOf(DR);
    }
    DataRow NewDR = dt.NewRow();
    if (Expression[0] == "add")
        dt.Rows.InsertAt(NewDR, Endindex + 1);
    else if (Expression[0] == "Sigular")
    {
        Endindex = 1;
    }
    else
        dt.Rows.Add(NewDR);
    DataRow level1Row = dtLevel1.NewRow();
    dtLevel1.Rows.Add(level1Row);
    if (startindex != null && Endindex != null)
    {
        for (int j = 0; j < dt.Columns.Count; j++)
        {
            string ColName = dt.Columns[j].ColumnName;
            if (ColName != "GLDescription" && ColName != "GLDescriptionId")
            {
                decimal totalRow = 0;
                if (Expression[0] == "add")
                {
                    for (int K = startindex; K <= Endindex; K++)
                    {
                        var DRValue = dt.Rows[K][j];
                        if (DRValue == null || DRValue == DBNull.Value)
                            DRValue = 0;
                        if (Expression[0].Contains("add"))
                            totalRow += Convert.ToDecimal(DRValue);
                    }
                }
                else if (Expression[0] == "Sigular")
                {
                    int KeyValue = startindex;
                    var DRValue = dt.Rows[KeyValue][j];
                    if (DRValue == null || DRValue == DBNull.Value)
                        DRValue = 0;
                    totalRow += Convert.ToDecimal(DRValue);
                }
                else
                {

                    int index = 0;
                    int paraindex = 0;
                    string rowName;
                    for (int P = 0; P < dt.Rows.Count; P++)
                    {                                
                            rowName = dt.Rows[P]["GLDescription"].ToString();

                            AddColume[0] = "";
                            if (AddColume.Contains(rowName))
                            {
                                int l = 1;

                                var DRValue = dt.Rows[P][j];

                                if (!(DRValue == null || DRValue == DBNull.Value))
                                {

                                        if (Expression[index] == "+")
                                        {
                                            totalRow += Convert.ToDecimal(DRValue);
                                        }
                                        else if (Expression[index] == "-")
                                        {
                                            totalRow -= Convert.ToDecimal(DRValue);
                                        }

                                    index++;                                            
                                }
                            }
                            else if (AddColume.Contains(rowName) && AddColume.Contains("self"))
                            {
                                var DRValue = dt.Rows[P][j];
                                if (!(DRValue == null || DRValue == DBNull.Value))
                                {
                                    decimal CurrentValue = Convert.ToDecimal(DRValue);
                                    dt.Rows[P][j] = CurrentValue * -1;
                                }
                            }


                    }

                }

                NewDR[ColName] = totalRow;
                level1Row[ColName] = totalRow;
            }
            else if (ColName == "GLDescription")
            {
                NewDR[ColName] = AddColume[0];
                level1Row[ColName] = AddColume[0];
            }
        }
    }
}
dtLevel2 = dt;

if (level == "lev1")
    return dtLevel1;
else if (level == "lev2")
    return dt;
else
    return null;

0 个答案:

没有答案