在for循环之后,DataTable会自行清除

时间:2014-05-26 02:59:53

标签: c# datatable

我正在使用数据表来存储一些数据。在for循环检查数据表的长度后,数据表将自行清除。其中没有数据。你能告诉它为什么会这样吗?这是使用DevExpress报告引擎创建报告的报告的一部分。

        DataTable ResultList = new DataTable();

        ResultList.Columns.Add("range");
        ResultList.Columns.Add("bpfrom");
        ResultList.Columns.Add("bpto");
        ResultList.Columns.Add("qualMemb");
        ResultList.Columns.Add("qualTurn");
        ResultList.Columns.Add("qualTurnPercent");
        ResultList.Columns.Add("grMemb");
        ResultList.Columns.Add("grTurn");
        ResultList.Columns.Add("grTurnPercent");
        ResultList.Columns.Add("totalamt");
        ResultList.Columns.Add("giftlevel");

        string[] fromrangelist = fromrange.Split(',');
        string[] torangelist = torange.Split(',');

        int j;

        if (fromrange == "")
        {
            DataRow dr = ResultList.NewRow();
            dr["range"] = 1;
            dr["bpfrom"] = 0;
            dr["bpto"] = 100000000; //biggest value
            dr["giftlevel"] = "ALL";
            ResultList.Rows.Add(dr);
        }
        else
        {
            for (j = 0; j < fromrangelist.Length; j++)
            {
                DataRow dr = ResultList.NewRow();
                dr["range"] = j + 1;
                dr["bpfrom"] = Convert.ToDecimal(fromrangelist[j]);
                dr["bpto"] = Convert.ToDecimal(torangelist[j]);
                dr["giftlevel"] = "level:" + (j + 1).ToString() + "(" + fromrangelist[j] + "-" + torangelist[j] + ")";  
                ResultList.Rows.Add(dr);
            }
        }

        //Get ALL Total spend 
        decimal TotalSpend = Math.Round((from table1 in context.mmsspddtls
                                         where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
                                          && table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
                                          && (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
                                          && (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
                                          && !table1.mmsspdstfg.Equals('V')
                                         select new
                                         {
                                             totalamt = table1.mmsspdpram
                                         }).Sum(s => s.totalamt), 2);

        for (int i = 0; i < ResultList.Rows.Count; i++)
        {
            decimal fromvalue = Convert.ToDecimal(ResultList.Rows[i]["bpfrom"]);
            decimal tovalue = Convert.ToDecimal(ResultList.Rows[i]["bpto"]);

            ResultList.Rows[i]["totalamt"] = TotalSpend;

            //Member Query that match the range
            var MemberQuery = (from table1 in context.mmscrdlogs
                               where table1.mmscdlcpcd.Equals(cpcd) && table1.mmscdlbrcd.Equals(brcd)
                               && table1.mmscdlstst.Equals('N') && table1.mmscdlbpis >= fromvalue && table1.mmscdlbpis <= tovalue
                               && table1.mmscdleddt >= fromexpiredate && table1.mmscdleddt <= toexpiredate
                               select new
                               {
                                   spendfr = fromvalue,
                                   spendto = tovalue,
                                   membercode = table1.mmscdlmbcd
                               }).Distinct();

            //Count of member in the range
            ResultList.Rows[i]["qualMemb"] = MemberQuery.Count();

            //member total spend in the range
            decimal qualTurn = 0;

            var qualQuery = (from table1 in context.mmsspddtls
                                from table2 in MemberQuery.Where(s => s.membercode == table1.mmsspdmbcd)
                                where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
                                && table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
                                && (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
                                && (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
                                && !table1.mmsspdstfg.Equals('V')
                                select new
                                {
                                    totalamt = table1.mmsspdpram
                                });

            if(qualQuery.Count() > 0)
            {
                qualTurn = Math.Round(qualQuery.Sum(s => s.totalamt), 2);
            }

            ResultList.Rows[i]["qualTurn"] = qualTurn;

            //member total spend% with total in the range

            if (qualTurn == 0)
                ResultList.Rows[i]["qualTurnPercent"] = 0;
            else
                ResultList.Rows[i]["qualTurnPercent"] = Math.Round((qualTurn / TotalSpend) * 100, 2);


            var MemberQueryGR = (from table1 in context.mmsspddtls
                                 from table2 in context.pxppludpxes.Where(s => s.pxppdxcpcd.Equals(table1.mmsspdcpcd) && s.pxppdxbrcd.Equals(table1.mmsspdbrcd) && s.pxppdxplcd.Equals(table1.mmsspdplcd))
                                 from table3 in context.mmssyspars.Where(s => s.mmssypcpcd.Equals(table1.mmsspdcpcd) && s.mmssypbrcd.Equals(table1.mmsspdbrcd))
                                 where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
                                 && table2.pxppdxpx01 / (table3.mmssypbpmt / table3.mmssyppont) >= fromvalue
                                 && table2.pxppdxpx01 / (table3.mmssypbpmt / table3.mmssyppont) <= tovalue
                                 && table1.mmsspdtxty.Equals("GR") && table1.mmsspdplna.Equals('2')
                                 && table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
                                 select new
                                 {
                                     spendfr = fromvalue,
                                     spendto = tovalue,
                                     membercode = table1.mmsspdmbcd
                                 }).Distinct();

            //Count of GR member in the range
            ResultList.Rows[i]["grMemb"] = MemberQueryGR.Count();

            //GR member total spend in the range
            decimal grTurn = 0;

            if (MemberQueryGR.Count() > 0)
            {
                var grQuery = (from table1 in context.mmsspddtls
                          from table2 in MemberQueryGR.Where(s => s.membercode == table1.mmsspdmbcd)
                          where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
                          && table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
                          && (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
                          && (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
                          && !table1.mmsspdstfg.Equals('V')
                          select new
                          {
                              totalamt = table1.mmsspdpram
                          });

                if(grQuery.Count() > 0)
                {
                    grTurn = Math.Round(grQuery.Sum(s => s.totalamt), 2);
                }
            }

            ResultList.Rows[i]["grTurn"] = grTurn;


            //member total spend% with total in the range

            if (grTurn == 0)
                ResultList.Rows[i]["grTurnPercent"] = 0;
            else
                ResultList.Rows[i]["grTurnPercent"] = Math.Round((grTurn / TotalSpend) * 100, 2);


        }                 



        DataSource = ResultList;
    }

请指教。感谢

0 个答案:

没有答案