当前位置:网站首页>DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
DataTable使用Linq进行分组汇总,将Linq结果集转化为DataTable
2022-08-04 04:42:00 【 浊尘】
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();
// 保存列集合的属性信息数组
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;//安全性检查
//循环遍历集合,使用反射获取类型的属性信息
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 ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
//获取泛型类型的参数
colType = colType.GetGenericArguments()[0];
}
//将类型的属性名称与属性类型作为DataTable的列数据
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
//新建一个用于添加到DataTable中的DataRow对象
DataRow dr = dtReturn.NewRow();
//循环遍历属性集合
foreach (PropertyInfo pi in oProps)
{
//为DataRow中的指定列赋值
dr[pi.Name] = pi.GetValue(rec, null) == null ?
DBNull.Value : pi.GetValue(rec, null);
}
//将具有结果值的DataRow添加到DataTable集合中
dtReturn.Rows.Add(dr);
}
return dtReturn;//返回DataTable对象
}
边栏推荐
猜你喜欢

七夕节,我用代码制作了表白信封

将xml标签转换为txt(voc格式转换为yolo方便进行训练)

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

A Preliminary Study of RSS Subscription to WeChat Official Account-feed43

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

汇编语言之栈

Learn iframes and use them to solve cross-domain problems

drools from download to postman request success

SQL interview Questions
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
随机推荐
There is an 8 hour difference between the docker installation of mysql and the host.
马尔可夫链
7-1 LVS+NAT 负载均衡群集,NAT模式部署
Basic characteristics of TL431 and oscillator circuit
OpenGL绘制一个圆锥
Learn iframes and use them to solve cross-domain problems
使用serve搭建本地服务器
7-3 LVS+Keepalived Cluster Description and Deployment
【流程图】
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Mini program + e-commerce, fun new retail
if,case,for,while
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
【21天学习挑战赛】顺序查找
数据治理平台项目总结和分析
drools from download to postman request success
Introduction and application of go module
【C语言进阶】程序环境和预处理
Mobile payment online and offline payment scenarios
初识Numpy