本文整理汇总了C#中DataTable.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.Sum方法的具体用法?C# DataTable.Sum怎么用?C# DataTable.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataTable
的用法示例。
在下文中一共展示了DataTable.Sum方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildRepoert
public void BuildRepoert()
{
var items = new List<SettlementDetails>
{
new SettlementDetails
{
Name = "A",
Amount = 1m,
},
new SettlementDetails
{
Name = "B",
Amount = 2m,
Amount1 = 2,
},
new SettlementDetails
{
Name = "C",
Amount = 3m,
Amount1 = 1,
}
};
var target = new DataTable<SettlementDetails>();
target.Column("Name", s => s.Name);
//計算列
IColumn colum4CalculateRow1 = target.Column("Amount0", s => s.Amount).Sum<decimal?>();
IColumn colum4CalculateRow2 = target.Column("Amount1", s => s.Amount1).Sum<decimal?>();
target.Sum<decimal?>("Amount+Amount1", colum4CalculateRow1, colum4CalculateRow2);
target.SetData(items);
var columns = new[] { "Name", "Amount0", "Amount1", "Amount+Amount1" };
int index = 0;
//check column Name;
foreach (IColumn column in target.Columns)
{
Assert.AreEqual(columns[index], column.Name);
index++;
}
//check the row item0;
index = 0;
foreach (var item in target.GetRows())
{
Assert.AreEqual(items[index].Name, item[0]);
Assert.AreEqual(items[index].Amount, item[1]);
Assert.AreEqual(items[index].Amount1, item[2]);
Assert.AreEqual((items[index].Amount ?? 0) + (items[index].Amount1 ?? 0), item[3], "row is " + index);
index++;
}
Assert.AreEqual(items.Sum(item => item.Amount), (colum4CalculateRow1).SumResult());
Assert.AreEqual(items.Sum(item => item.Amount1), (colum4CalculateRow2).SumResult());
}
示例2: BuildRepoert_empty
public void BuildRepoert_empty()
{
var target = new DataTable<SettlementDetails>();
target.Column("Name", s => s.Name);
//計算列
IColumn colum4CalculateRow1 = target.Column("Amount0", s => s.Amount).Sum<decimal?>();
IColumn colum4CalculateRow2 = target.Column("Amount1", s => s.Amount1).Sum<decimal?>();
target.Sum<decimal?>("Amount+Amount1", colum4CalculateRow1, colum4CalculateRow2);
target.SetData(new List<SettlementDetails>());
target.GetRows();
Assert.AreEqual(null, colum4CalculateRow1.SumResult());
}