How Do I Use Gruop By Method In Charp Code To Covert DataTable? 分类汇总问题请教!
2014-05-15 来自:雨后星晴 1 人回应

 

 

 

private System.Data.DataTable TestDataTable()
    {
        System.Data.DataTable oldDataTable = new DataTable();
        oldDataTable.Columns.Add("CheckinNum");
        oldDataTable.Columns.Add("CustomerAmount");
        oldDataTable.Columns.Add("ProviderAmount");
        oldDataTable.Columns.Add("AgentAmount");
        oldDataTable.Columns.Add("GrossAmount");
        oldDataTable.Columns.Add("TheMonth");

        int N=17; //N行数据;

        System.Data.DataRow dr = oldDataTable.NewRow(); 
        for (int i = 1; i < N; i++)
        {
            dr = oldDataTable.NewRow();
            dr["CheckinNum"] = 1;
            dr["CustomerAmount"] = 2;
            dr["ProviderAmount"] = 3;
            dr["AgentAmount"] = 4;
            dr["GrossAmount"] = 5;
            dr["TheMonth"] = i<=12?i:1;
            oldDataTable.Rows.Add(dr);
        }

        return oldDataTable;

        //转换思路:把oldDataTable按1到12进行分类汇总,就是卡在这里了...,汇总用foreach()
        //形如:                    
        /*
            foreach (DataRow dr in oldDataTable.Rows)        //关键点:根据(1 到12 月分来分类汇总,这里要怎样处理??
        {
            int CheckinNum = Convert.ToInt32(dr["CheckinNum"]);
            this.totalCheckinNum += CheckinNum;
            ........................................
        }
        */
    }
2014-05-20 来自:琴弦

Your have more questions!

Why not using sql statement to finish it?

You can use Linq method,for example 'GroupBy'!

您的回应

你还未登陆,不能回应!登陆