当前位置:网站首页>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对象
}
边栏推荐
猜你喜欢
随机推荐
深度学习之 10 卷积神经网络3
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
【id类型和NSObject指针 ObjectIve-C中】
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
2022年PMP考试延迟了,该喜该忧?
if,case,for,while
如何动态添加script依赖的脚本
数据治理平台项目总结和分析
拿捏JVM性能优化(自己笔记版本)
震惊,99.9% 的同学没有真正理解字符串的不可变性
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
How to dynamically add script dependent scripts
附加:对于“与数据表对应的实体类“,【面对MongoDB时,使用的@Id等注解】和【以前面对MySQL时,使用的@Id等注解】,是不同的;
10 Convolutional Neural Networks for Deep Learning 3
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
redis中常见的面试题
企业直播风起:目睹聚焦产品,微赞拥抱生态
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
System design. Seckill system
7-3 LVS+Keepalived集群叙述与部署