当前位置: 首页>>代码示例>>C#>>正文


C# DataTable.Sum方法代码示例

本文整理汇总了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());
        }
开发者ID:luqizheng,项目名称:Qi4Net,代码行数:53,代码来源:DataTableTest.cs

示例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());
        }
开发者ID:luqizheng,项目名称:Qi4Net,代码行数:13,代码来源:DataTableTest.cs


注:本文中的DataTable.Sum方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。