简体   繁体   中英

C# How to add total row into datatable

I have a table like this

BookingDate  Status  BookingNumber  
----------------------------------   
2016/10/14    1         3  
2016/10/14    1         1  
2016/10/14    1         5  
2016/10/13    1         7  
2016/10/13    1         4  
2016/10/12    1         8  
2016/10/12    1         3  
2016/10/12    1         6  
2016/10/12    1         2

I want group by BookingDate and count BookingNumber. Add new row into datatable

BookingDate  Status  BookingNumber
    ----------------------------------------
    2016/10/14    1         3
    2016/10/14    1         1
    2016/10/14    1         5
    2016/10/14   Total      9
    2016/10/13    1         7
    2016/10/13    1         4
    2016/10/14   Total      11
    2016/10/12    1         8
    2016/10/12    1         3
    2016/10/12    1         6
    2016/10/12    1         2
    2016/10/14   Total      19

Here is my LinqPad example code:

    void Main()
{
    DataTable workTable = new DataTable("Order");
    DataColumn workCol = workTable.Columns.Add("BookingDate", typeof(String));
    workTable.Columns.Add("Status", typeof(String));
    workTable.Columns.Add("BookingNumber", typeof(int));
    workTable.Rows.Add("2016/10/14","1",3);
    workTable.Rows.Add("2016/10/14","1",1);
    workTable.Rows.Add("2016/10/14","1",5);
    workTable.Rows.Add("2016/10/13","1",7);
    workTable.Rows.Add("2016/10/13","1",4);
    workTable.Rows.Add("2016/10/12","1",8);
    workTable.Rows.Add("2016/10/12","1",3);
    workTable.Rows.Add("2016/10/12","1",6);
    workTable.Rows.Add("2016/10/12","1",2);
    workTable.AsEnumerable().GroupBy(x=>x.Field<string>("BookingDate")).Dump();
}

I don't know Linq is able to to this? Or just use for loop to insert new row.

Personally, I wouldn't store this in the same table. Instead, I would have a separate table (maybe called OrderTotals?) that tracked booking totals. Each order could have a foreign key referencing the primary key of its corresponding totals record.

But if you really want to store totals in the same table, then I would define an extra, optional column for it.

I finally solve this problem.

void Main()
{
    DataTable workTable = new DataTable("Order");

    DataColumn workCol = workTable.Columns.Add("BookingDate", typeof(String));
    workTable.Columns.Add("Status", typeof(String));
    workTable.Columns.Add("BookingNumber", typeof(int));
    workTable.Rows.Add("2016/10/14","1",3);
    workTable.Rows.Add("2016/10/14","1",1);
    workTable.Rows.Add("2016/10/14","1",5);
    workTable.Rows.Add("2016/10/13","1",7);
    workTable.Rows.Add("2016/10/13","1",4);
    workTable.Rows.Add("2016/10/12","1",8);
    workTable.Rows.Add("2016/10/12","1",3);
    workTable.Rows.Add("2016/10/12","1",6);
    workTable.Rows.Add("2016/10/12","1",2);

    //========統計人數
var count=workTable.AsEnumerable()
.Select(x=>new { BookingDate= x.Field<string>("BookingDate"),BookingNumber = x.Field<int>("BookingNumber")})
.GroupBy(x=>x.BookingDate)
.Select(x=>new{BookingDate=x.Key,BookingNumber=x.Sum(s=>s.BookingNumber),Index=x.Count()}).OrderByDescending(x=>x.BookingDate).ToList();

count.Dump();
//========插入新的row
int Position=0;
foreach(var obj in count){
    DataRow dr = workTable.NewRow();

    dr[0] = obj.BookingDate;
    dr[1] = "Total";
    dr[2] = obj.BookingNumber;
    Position +=obj.Index;
    workTable.Rows.InsertAt(dr, Position);
    Position++;
}

workTable.Dump();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM