C#用DataTable实现Group by数据统计
group t by new { t1 = t.Field< string >( " name " ), t2 = t.Field< string >( " sex " ) } into m
select new
{
name = m.Key.t1,
sex = m.Key.t2,
score = m.Sum(n => n.Field< decimal >( " score " ))
};
if (query.ToList().Count > 0 )
{
query.ToList().ForEach(q =>
{
Console.WriteLine(q.name + " , " + q.sex + " , " + q.score);
});
发布日期:2021-11-15 03:27:10
浏览次数:4
分类:技术文章
本文共 755 字,大约阅读时间需要 2 分钟。
使用linq to DataTable group by实现
var query = from t in dt.AsEnumerable()group t by new { t1 = t.Field< string >( " name " ), t2 = t.Field< string >( " sex " ) } into m
select new
{
name = m.Key.t1,
sex = m.Key.t2,
score = m.Sum(n => n.Field< decimal >( " score " ))
};
if (query.ToList().Count > 0 )
{
query.ToList().ForEach(q =>
{
Console.WriteLine(q.name + " , " + q.sex + " , " + q.score);
});
}
衍生使用
public class Student
{
public string name {get;set;}
public string sex {get;set;}
public decimal score {get;set;}
}
List<Student> query = from t in dt.AsEnumerable()
group t by new { t1 = t.Field<string>("name"), t2 = t.Field<string>("sex") } into m
select new
Student
{
name = m.Key.t1,
sex = m.Key.t2,
score = m.Sum(n => n.Field<decimal>("score"))
};
转载地址:https://blog.csdn.net/qq_30469045/article/details/51540873 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月31日 19时01分34秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Linux服务器网络开发模型
2019-04-27
nginx虚拟目录设置 alias 和 root
2019-04-27
理解http响应头中的Date和Age
2019-04-27
四层和七层负载均衡的区别
2019-04-27
设置Squid Cache_mem大小
2019-04-27
squid日志文件太大,怎样处理?
2019-04-27
让Squid 显示本地时间
2019-04-27
linux mysql 命令 大全
2019-04-27
清除Squid缓存的小工具
2019-04-27
Varnish Cache 3.0.0安装
2019-04-27
深入探讨Varnish缓存命中率
2019-04-27
Linux下文件如果没有权限不能被Apache访问
2019-04-27
Linux内核学习四库全书
2019-04-27
Linux内核模块编程入门
2019-04-27
使用Cacti监控你的网络Cacti的安装
2019-04-27
2011年6月编程语言关注度排行
2019-04-27
Varnish使用小结
2019-04-27
千万级并发HAproxy均衡负载系统介绍
2019-04-27
什么是A记录、MX记录、CNAME记录
2019-04-27