简体   繁体   English

C#如何将总行添加到数据表

[英]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. 我要按BookingDate分组并计数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: 这是我的LinqPad示例代码:

    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? 我不知道Linq能够做到这一点吗? Or just use for loop to insert new row. 或仅使用for循环插入新行。

Personally, I wouldn't store this in the same table. 就个人而言,我不会将其存储在同一张表中。 Instead, I would have a separate table (maybe called OrderTotals?) that tracked booking totals. 相反,我将有一个单独的表(可能称为OrderTotals?)来跟踪预订总数。 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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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