Linq实现DataTable的分组统计
DataTable dt = GetTestData(10); //获取10条测试数据
var queryByService = from r in dt.AsEnumerable()
group r by r.Field<string>(4) into g
select new
{
Service = g.Key,
Bookings = g.Count(p => p.Field<string>(1) !=""),
ConfirmedBookings = g.Count(p => p.Field<string>(1) =="Confirmed"),
PendingBookings = g.Count(p => p.Field<string>(1) =="Pending"),
CancelledBookings = g.Count(p => p.Field<string>(1) =="Cancelled")
};
var queryByClient = from r in dt.AsEnumerable()
where r.Field<string>(1) =="Confirmed"
group r by r.Field<string>(5) into g
select new
{
BookingClient = g.Key,
_20DV = g.Count(p => p.Field<string>(2)=="20DV"),
_40DV = g.Count(p => p.Field<string>(2) =="40DV"),
_40HC = g.Count(p => p.Field<string>(2) =="40HC"),
Bookings = g.Count(),
TotalOceanFreight = g.Sum(p => p.Field<decimal>(3)),
AverageOceanFreight = g.Average(p => p.Field<decimal>(3))
};
var queryByType = from r in dt.AsEnumerable()
group r by r.Field<string>(2) into g
select new
{
EquipmentType = g.Key,
Total = g.Count(),
Confirmed = g.Count(p => p.Field<string>(1) =="Confirmed"),
Pending = g.Count(p => p.Field<string>(1) =="Pending"),
Cancelled = g.Count(p => p.Field<string>(1) =="Cancelled")
};
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = queryByService;
GridView2.DataBind();
GridView3.DataSource = queryByClient;
GridView3.DataBind(); DataTable dtByType = ConvertToDataTable(queryByType);
GridView4.DataSource = dtByType; //或 GridView4.DataSource = queryByType;
GridView4.DataBind();
另外ConvertToDataTable()是在网上看到的方法
public DataTable ConvertToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn =new DataTable();
// column names
PropertyInfo[] oProps =null;
if (varlist ==null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps ==null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() ==typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) ==null? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
Linq实现DataTable的分组统计的更多相关文章
- 如何使用linq操作datatable进行分组
使用微软.net的孩子们应该都知道linq吧,要知道linq可是其他高级语言没有的技术,比如php,java等等,但是起初我对linq的认识只是停留在对 list<> 的泛型集合进行操作, ...
- DataTable、List使用groupby进行分组和分组统计;List、DataTable查询筛选方法
DataTable分组统计: .用两层循环计算,前提条件是数据已经按分组的列排好序的. DataTable dt = new DataTable(); dt.Columns.AddRange(new ...
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
2013-9-15 1. CustomValidator验证控件验证用户输入的字符长度 在实际的开发中通常会遇到验证用户输入的字符长度的问题,通常的情况下,可以写一个js的脚本或者函数,在ASP ...
- (转)C#用Linq实现DataTable的Group by数据统计
本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序 ...
- C# Linq及Lamda表达式实战应用之 GroupBy 分组统计
在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...
- Dev用于界面按选中列进行分组统计数据源(实用技巧)
如果有用U8的可以明白这个功能就是模仿他的统计功能.我不过是把他造成通用的与适应于DEV的. (效率为6000条数据分组统计时间为3秒左右分组列过多5秒.1000条以下0.几秒,500条下0.00几秒 ...
- XtraGrid使用心得(折叠式主细档、分组统计)
XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...
- 【转】Linq实现DataTable行列转换
出处:http://www.cnblogs.com/li-peng/ 转换前的table: 转换后的table: 代码里有详细的说明, 还有一些参数我都截图了下面有 using System;usin ...
随机推荐
- JS 模板引擎之JST模板
项目中有用到JST模板引擎,于是抽个时间出来,整理了下关于JST模板引擎的相关内容. 试想一个场景,当点击页面上列表的翻页按钮后,通过异步请求获得下一页的列表数据并在页面上显示出来.传统的JS做法是编 ...
- Spring MVC实现文件下载
下载文件① 下载文件需要将byte数组还原成文件. 首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式).然后使用OutputStream将文件输入 @RequestMapp ...
- [CareerCup] 6.1 Find Heavy Bottle 寻找重瓶子
6.1 You have 20 bottles of pills. 19 bottles have 1.0 gram pills, but one has pills of weight 1.1 gr ...
- [CareerCup] 8.7 Chat Server 聊天服务器
8.7 Explain how you would design a chat server. In particular, provide details about the various bac ...
- Win10开发究竟能实现哪些牛逼的功能
经Win10开发者群(53078485)大咖Aran童鞋授权,这次先Show一下他通过vs2015做的跨端APP一些高级功能的GIF图,大家可以回帖想要哪个功能的DEMO,我和Aran说一下,会把DE ...
- 『随笔』C# 程序 修改 ConfigurationManager 后,不重启 刷新配置
基本共识: ConfigurationManager 自带缓存,且不支持 写入. 如果 通过 文本写入方式 修改 配置文件,程序 无法刷新加载 最新配置. PS. Web.config 除外:Web. ...
- 如何优雅的写一篇安利文-以Sugar ORM为例
前言 我最近喜欢把写的十分优美的技术文章叫做安利文.首先,文章必须是原创而非软广:其次,阅读之后不仅能快速吸纳技术要点并入门开发,还能感同身受的体会作者热情洋溢的赞美和急于分享心得体验的心情,让人感觉 ...
- MVVM开源框架Knot.js 教程2 - 大幅改变前端框架开发体验的Debugger
Knotjs教程系列 1.CBS初步 2.Knot.js Debugger(本文) ....持续增加中 Knot.js 教程2 - 改变前端框架开发体验的Debugger Debugger只是一个方便 ...
- 嵌入式linux驱动开发之给linux系统添加温度传感器模块
忙了几天,终于可以让ds18b20在自己的开发板的linux系统上跑了!虽然ds18b20不是什么新鲜玩意,但是想想知己可以给linux系统添加模块了还是有点小鸡冻呢! 虽然说现在硬件的资源非常丰富而 ...
- Bootstrap系列 -- 20. 禁用状态
Bootstrap框架的表单控件的禁用状态和普通的表单禁用状态实现方法是一样的,在相应的表单控件上添加属性“disabled” 在使用了“form-control”的表单控件中,样式设置了禁用表单背景 ...