Previous topicNext topic
Help > 开发指南 > SanMuGrid平台编程 > 主要对象 > DataTableHelp > 数据处理 >
GetGroupDictionary

GetGroupDictionary依据某个单列对某一列进行统计,结果以字典的形式返回。 我们可以指定返回字典的Key、Value的数据类型。 这个函数的功能类似于GroupBy函数,只是这里只针对一列字段进行统计,并且返回的是一个字典而已。

语法:

GetGroupDictionary<TKey,TValue>(groupColName,agreegateColName,aggregateType)

参数说明

名称 说明
groupColName 必填项,字符串类型,分组依据列名称。这里也只能输入一个列名。
agreegateColName 必填项,字符串类型,需要统计的列名称。这里只可以输入一个统计列。如果需要统计多列的,请使用PivotDataTable。
aggregateType 可选项,AggregateTypeEnum类型枚举,统计的类型。默认值为Sum。

我们还是针对这样的数据进行测试。

Vb.Net
Dim tbl As SmGrid=Proj.CurrentSmGrid
'获取求和字典
Dim dic As Dictionary(Of String,Integer)=tbl.DataTableHelp.GetGroupDictionary(Of String,Integer)("产品","数量",AggregateTypeEnum.Sum)
'遍历字典并显示
For Each item As KeyValuePair(Of String,Integer)In dic
    Proj.MsgDebug.Add(item.Key+"|"+item.Value.ToString())
Next
'获取计数字典
dic=tbl.DataTableHelp.GetGroupDictionary(Of String,Integer)("产品","数量",AggregateTypeEnum.Count)
'遍历字典并显示
For Each item As KeyValuePair(Of String,Integer)In dic
    Proj.MsgDebug.Add(item.Key+"|"+item.Value.ToString())
Next

'返回结果:产品01|214646
'返回结果:产品02|222197
'返回结果:产品03|216428
'返回结果:产品04|217996
'返回结果:产品05|229748
'返回结果:产品01|380
'返回结果:产品02|395
'返回结果:产品03|388
'返回结果:产品04|418
'返回结果:产品05|419

C#
SmGrid tbl = Proj.CurrentSmGrid;
// 获取求和字典
Dictionary<string, int> dic = tbl.DataTableHelp.GetGroupDictionary<string, int>("产品", "数量", AggregateTypeEnum.Sum);
// 遍历字典并显示
foreach (KeyValuePair<string, int> item in dic)
    Proj.MsgDebug.Add(item.Key + "|" + item.Value.ToString());
// 获取计数字典
dic = tbl.DataTableHelp.GetGroupDictionary<string, int>("产品", "数量", AggregateTypeEnum.Count);
// 遍历字典并显示
foreach (KeyValuePair<string, int> item in dic)
    Proj.MsgDebug.Add(item.Key + "|" + item.Value.ToString());
    
//返回结果:产品01|214646
//返回结果:产品02|222197
//返回结果:产品03|216428
//返回结果:产品04|217996
//返回结果:产品05|229748
//返回结果:产品01|380
//返回结果:产品02|395
//返回结果:产品03|388
//返回结果:产品04|418
//返回结果:产品05|419