我在计算像DataTable
这样的数据结构中一列中所有先前行的总和时遇到了一些麻烦
例如,我们有一个表,其中有“等待时间” 和“ Burst Time” 。
等待时间的行值必须通过获取“爆发时间”列中前几行的值来计算(如果又不是我们当前正在计算值的第一行,则为)>
我该如何实现?是否像使用Compute()
并传递适当的过滤器字符串一样简单?
答案 0 :(得分:0)
我举了一个例子。现在已经知道如何填充DataTable,这就是为什么我想出一个硬编码的数据表。
id="myCheckbox"
我想对您来说最有趣的是:
public class Program
{
public static void Main(string[] args)
{
dataSet = new DataSet();
var dataTable = MakeTable();
var columns = dataTable.Columns;
Console.WriteLine("Defined Table");
PrintDataTable(dataTable);
foreach (DataRow dataTableRow in dataTable.Rows)
{
var id = dataTableRow["ID"];
var burstTime = dataTable.Compute("Sum(BurstTime)", $"ID < {id}");
dataTableRow["WaitingTime"] = burstTime;
}
Console.WriteLine();
Console.WriteLine("Table After Operation");
PrintDataTable(dataTable);
Console.ReadLine();
}
private static void PrintDataTable(DataTable dataTable)
{
foreach (DataRow dataTableRow in dataTable.Rows)
{
Console.WriteLine($"{dataTableRow["Id"]}\t\t| {dataTableRow["WaitingTime"]}\t\t| {dataTableRow["BurstTime"]}");
}
}
private static DataSet dataSet;
private static DataTable MakeTable()
{
// Create a new DataTable.
DataTable table = new DataTable("childTable");
DataColumn column;
DataRow row;
// Create first column and add to the DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ID";
column.AutoIncrement = true;
column.Caption = "ID";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ChildItem";
column.AutoIncrement = false;
column.Caption = "ChildItem";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ParentID";
column.AutoIncrement = false;
column.Caption = "ParentID";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "WaitingTime";
column.AutoIncrement = false;
column.Caption = "WaitingTime";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
// Create fourth column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "BurstTime";
column.AutoIncrement = false;
column.Caption = "BursTime";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column);
dataSet.Tables.Add(table);
// Create three sets of DataRow objects,
// five rows each, and add to DataTable.
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["ID"] = i;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 0;
row["WaitingTime"] = 0;
row["BurstTime"] = i;
table.Rows.Add(row);
}
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["ID"] = i + 5;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 1;
row["WaitingTime"] = 0;
row["BurstTime"] = i + 5;
table.Rows.Add(row);
}
for (int i = 0; i <= 4; i++)
{
row = table.NewRow();
row["ID"] = i + 10;
row["ChildItem"] = "Item " + i;
row["ParentID"] = 2;
row["WaitingTime"] = 0;
row["BurstTime"] = i + 10;
table.Rows.Add(row);
}
return table;
}
}
我认为,这里最重要的是,您必须提出自己的比较逻辑,即foreach (DataRow dataTableRow in dataTable.Rows)
{
var id = dataTableRow["ID"];
var burstTime = dataTable.Compute("Sum(BurstTime)", $"ID < {id}");
dataTableRow["WaitingTime"] = burstTime;
}
函数中的第二个参数。