当前位置:网站首页>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对象
}
边栏推荐
猜你喜欢
if,case,for,while
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
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
张量篇-应用案例
机器学习之视频学习【更新】
RSS订阅微信公众号初探-feed43
Eight guiding principles to help businesses achieve digital transformation success
Converts XML tags to TXT format (voc conversion for yolo convenient training)
For Qixi Festival, I made a confession envelope with code
随机推荐
劝退背后。
docker安装mysql与宿主机相差8小时的问题。
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
文件系统的简单操作
7-3 LVS+Keepalived Cluster Description and Deployment
Learn iframes and use them to solve cross-domain problems
类如何只能静态分配和只能动态分配
How to dynamically add script dependent scripts
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
Implementing a server-side message active push solution based on SSE
关于yolo7和gpu
2022 software test interview questions The latest ByteDance 50 real interview questions, 15k have been won after brushing, with explanation + Q&A
PL/SQL Some Advanced Fundamental
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
JVM的内存模型简介
3000字,一文带你搞懂机器学习!
System design. Seckill system
Introduction to mq application scenarios
unity框架之缓存池
Oracle与Postgresql在PLSQL内事务回滚的重大差异