当前位置:网站首页>DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
2022-08-04 04:45:00 【turbid dust】
DataTable使用Linq进行分组汇总
var dt1=dt.AsEnumerable();
var result1= from r in dt1
group r by new {
FPJCABASENUMBER = r.Field<string>("FPJCABASENUMBER"),
FMATERIALNUMBER2 = r.Field<string>("FMATERIALNUMBER2") ,
FINVFBaseQty=r.Field<decimal>("FINVFBaseQty"),
FSAFESTOCK = r.Field<decimal>("FSAFESTOCK"),
//FMUSTQTY = r.Field<decimal>("FMUSTQTY"),
//FQUELIAO = r.Field<decimal>("FQUELIAO"),
//FPOORECEIVEQTY = r.Field<decimal>("FPOORECEIVEQTY"),
//FTPINSTOCKFQTY = r.Field<decimal>("FTPINSTOCKFQTY"),
//FPOPRENOSTOCKQTY = r.Field<decimal>("FPOPRENOSTOCKQTY"),
//FTPMEFQTY = r.Field<decimal>("FTPMEFQTY"),
}
into m
select new
{
FPJCABASENUMBER = m.Key.FPJCABASENUMBER,
FMATERIALNUMBER2 = m.Key.FMATERIALNUMBER2,
FINVFBaseQty = m.Key.FINVFBaseQty,
FSAFESTOCK = m.Key.FSAFESTOCK,
FMUSTQTY = m.Sum(x=>x.Field<decimal>("FMUSTQTY")),
FQUELIAO = m.Sum(x=>x.Field<decimal>("FQUELIAO")),
FPOORECEIVEQTY = m.Sum(x=>x.Field<decimal>("FPOORECEIVEQTY")),
FTPINSTOCKFQTY = m.Sum(x=>x.Field<decimal>("FTPINSTOCKFQTY")),
FPOPRENOSTOCKQTY = m.Sum(x=>x.Field<decimal>("FPOPRENOSTOCKQTY")),
FTPMEFQTY = m.Sum(x=>x.Field<decimal>("FTPMEFQTY")),
};
var resultTable = LINQToDataTable(result1);
将Linq结果集转化为DataTable
/// <summary>
/// 将IEnumerable<T>类型的集合转换为DataTable类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="varlist"></param>
/// <returns></returns>
public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
//定义要返回的DataTable对象
DataTable dtReturn = new DataTable();
// An array of property information that holds the column collection
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;//安全性检查
//循环遍历集合,Use reflection to get property information of a type
foreach (T rec in varlist)
{
//使用反射获取T类型的属性信息,返回一个PropertyInfo类型的集合
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
//循环PropertyInfo数组
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;//得到属性的类型
//If the property is a generic type
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
//获取泛型类型的参数
colType = colType.GetGenericArguments()[0];
}
//Take the property name of the type with the property type asDataTable的列数据
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
//Create a new one to add toDataTable中的DataRow对象
DataRow dr = dtReturn.NewRow();
//Loop through the property collection
foreach (PropertyInfo pi in oProps)
{
//为DataRowThe specified column assignment in
dr[pi.Name] = pi.GetValue(rec, null) == null ?
DBNull.Value : pi.GetValue(rec, null);
}
//will have the resulting value ofDataRow添加到DataTable集合中
dtReturn.Rows.Add(dr);
}
return dtReturn;//返回DataTable对象
}
边栏推荐
猜你喜欢
![[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]](/img/f7/78eea9f14ca97b5e78592c7c2be313.png)
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]

某母婴小程序加密参数解密

el-Select 选择器 底部固定

Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model

7-3 LVS+Keepalived集群叙述与部署

看DevExpress丰富图表样式,如何为基金公司业务创新赋能

System design. Seckill system

Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis

leetcode 12. 整数转罗马数字

Take care of JVM performance optimization (own note version)
随机推荐
SVM介绍以及实战
XSS related knowledge points
杭电多校-Slipper-(树图转化+虚点建图)
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
【21 Days Learning Challenge】Direct Insertion Sort
JVM的内存模型简介
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
leetcode 12. 整数转罗马数字
【流程图】
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
【云原生--Kubernetes】Pod资源管理与探针检测
小程序 + 电商,玩转新零售
Jenkins 导出、导入 Job Pipeline
7. The principle description of LVS load balancing cluster
if,case,for,while
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
七夕节,我用代码制作了表白信封
基于 SSE 实现服务端消息主动推送解决方案
docker安装mysql与宿主机相差8小时的问题。
机器学习之视频学习【更新】