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 ...
随机推荐
- [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
4.6 Write an algorithm to find the'next'node (i.e., in-order successor) of a given node in a binary ...
- Jenkins进阶系列之——09配置Linux系统ssh免密码登陆
ssh认证的完整描述:https://www.ibm.com/developerworks/cn/linux/security/openssh/part1/ 说明:点我去查看 今天我们只说生成ssh的 ...
- 自己写了个H5版本的俄罗斯方块
在实习公司做完项目后,实在无聊.就用H5写了几个游戏出来玩一下.从简单的做起,就搞了个经典的俄罗斯方块游戏. 先上效果: 上面的数字是得分,游戏没有考虑兼容性,只在chrome上测试过,不过大部分现代 ...
- setTimeout 0秒
我们通常知道常用setTimeout 0秒来解决动画或者一些效果的延迟问题:众所周知js是单线程,用0秒能把要执行的任务从队列中提出来.其实我也不太懂 有这个问题alert(1);setTimeout ...
- ipvsadm参数详解(常用命令)
[root@localhost ipvsadm]# ipvsadm -h ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1) U ...
- 读代码之htmlParser
在以前使用HtmlParser时,并未考虑过遇到org.htmlparser.tags之外的Tag怎么处理.直到碰到这样的一个标签,如果不加处理,HtmlParser无法对其进行处理.查阅自定义标签之 ...
- Beta版本贡献比
学号 姓名 贡献率 031302301 毕容甲 25%031302302 蔡逸轩 25%031302430 肖阳 25%031302418 黄彦 ...
- 史上最全的HTML、CSS知识点总结,浅显易懂。
来源于:http://blog.csdn.net/qiushi_1990/article/details/40260447 一,html+css基础1-1Html和CSS的关系学习web前端开发基础技 ...
- ORACLE建表练习
1,学生表 -- Create table create table T_HQ_XS ( xueh ) not null, xingm ) not null, xingb ) ', nianl NUM ...
- winform之判断验证码,,附加验证码的一般处理程序
这里用的一般处理程序画的验证码图片. 判断验证码步骤: ①先在一般处理程序中获取验证码(一般处理程序中session的调用→context.Session[]) string code = GetRn ...