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 ...
随机推荐
- 用python简单处理图片(4):图像中的像素访问
前面的一些例子中,我们都是利用Image.open()来打开一幅图像,然后直接对这个PIL对象进行操作.如果只是简单的操作还可以,但是如果操作稍微复杂一些,就比较吃力了.因此,通常我们加载完图片后,都 ...
- [CareerCup] 9.3 Magic Index 魔法序号
9.3 A magic index in an array A[0.. .n-1] is defined to be an index such that A[i] = i. Given a sort ...
- 实践GDB
调试工具简介:GDB Unix程序员最常用的调试工具是GDB,这是由Richard Stallman(开源软件运动的领路人) 开发的GNU项目调试器,该工具在Linux开发中扮演了关键的角色. CG ...
- IT应届生如何准备找工作?
今天和一个弟弟吃饭,他明年年初即将计算机研究生毕业.谈论到怎么找工作,觉得自己会的不多,心里非常发虚.虽然我当年找工作也走了很多弯路,思路并不是很清晰.但是工作了这么多年,对企业需要什么样子的人还是有 ...
- Ubuntu安装uget和aria2下载工具
Windows下的下载工具有迅雷,快车等.Ubuntu下也有强大的下载工具uget和aria2. 一.安装.uget和aria2都可以在“软件中心”中安装,但是版本太老啦,所以最好还是在终端中添加pp ...
- Jenkins入门系列之——00答疑解惑
写在最前的总结:Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的.比如你要获取Subversion上最新的源代码,Jenkins会去调用SVNKIT(插件的核心Jar ...
- CSS 居中大全(转)
转自这里,收藏备用. <center> 不建议用了. text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:mi ...
- [转载]使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
转载,原文来自 http://blog.csdn.net/hawksoft/article/details/21776009 最近调试原来的微信模拟登陆时发生了“基础连接已关闭,发送时发生错误”的错误 ...
- jquery设置滚动条距离页面顶部的高度
//获取滚动条到顶部的垂直高度 $(document).scrollTop(); //获取滚动条到左边的垂直宽度 $(document).scrollLeft(); function clickFn( ...
- Java数组的--遍历
(I)标准for循环遍历数组 例如代码片段: int [] nums = new int [] {0,1,2,3,4,5,6,7,8,9}; for(int i=0;i<11;i++){ Sys ...