1.  
  2. DataTable dt = GetTestData(10); //获取10条测试数据
  3. var queryByService = from r in dt.AsEnumerable()
  4. group r by r.Field<string>(4) into g
  5. select new
  6. {
  7. Service = g.Key,
  8. Bookings = g.Count(p => p.Field<string>(1) !=""),
  9. ConfirmedBookings = g.Count(p => p.Field<string>(1) =="Confirmed"),
  10. PendingBookings = g.Count(p => p.Field<string>(1) =="Pending"),
  11. CancelledBookings = g.Count(p => p.Field<string>(1) =="Cancelled")
  12. };
  13. var queryByClient = from r in dt.AsEnumerable()
  14. where r.Field<string>(1) =="Confirmed"
  15. group r by r.Field<string>(5) into g
  16. select new
  17. {
  18. BookingClient = g.Key,
  19. _20DV = g.Count(p => p.Field<string>(2)=="20DV"),
  20. _40DV = g.Count(p => p.Field<string>(2) =="40DV"),
  21. _40HC = g.Count(p => p.Field<string>(2) =="40HC"),
  22. Bookings = g.Count(),
  23. TotalOceanFreight = g.Sum(p => p.Field<decimal>(3)),
  24. AverageOceanFreight = g.Average(p => p.Field<decimal>(3))
  25. };
  26. var queryByType = from r in dt.AsEnumerable()
  27. group r by r.Field<string>(2) into g
  28. select new
  29. {
  30. EquipmentType = g.Key,
  31. Total = g.Count(),
  32. Confirmed = g.Count(p => p.Field<string>(1) =="Confirmed"),
  33. Pending = g.Count(p => p.Field<string>(1) =="Pending"),
  34. Cancelled = g.Count(p => p.Field<string>(1) =="Cancelled")
  35. };
  36. GridView1.DataSource = dt;
  37. GridView1.DataBind();
  38. GridView2.DataSource = queryByService;
  39. GridView2.DataBind();
  40. GridView3.DataSource = queryByClient;
  41. GridView3.DataBind();
  42.  
  43. DataTable dtByType = ConvertToDataTable(queryByType);
  44. GridView4.DataSource = dtByType; //或 GridView4.DataSource = queryByType;
  45. GridView4.DataBind();

另外ConvertToDataTable()是在网上看到的方法

  1.  
  2. public DataTable ConvertToDataTable<T>(IEnumerable<T> varlist)
  3. {
  4. DataTable dtReturn =new DataTable();
  5. // column names
  6. PropertyInfo[] oProps =null;
  7. if (varlist ==null) return dtReturn;
  8. foreach (T rec in varlist)
  9. {
  10. // Use reflection to get property names, to create table, Only first time, others will follow
  11. if (oProps ==null)
  12. {
  13. oProps = ((Type)rec.GetType()).GetProperties();
  14. foreach (PropertyInfo pi in oProps)
  15. {
  16. Type colType = pi.PropertyType;
  17. if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() ==typeof(Nullable<>)))
  18. {
  19. colType = colType.GetGenericArguments()[0];
  20. }
  21. dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
  22. }
  23. }
  24. DataRow dr = dtReturn.NewRow();
  25. foreach (PropertyInfo pi in oProps)
  26. {
  27. dr[pi.Name] = pi.GetValue(rec, null) ==null? DBNull.Value : pi.GetValue
  28. (rec, null);
  29. }
  30. dtReturn.Rows.Add(dr);
  31. }
  32. return dtReturn;
  33. }

Linq实现DataTable的分组统计的更多相关文章

  1. 如何使用linq操作datatable进行分组

    使用微软.net的孩子们应该都知道linq吧,要知道linq可是其他高级语言没有的技术,比如php,java等等,但是起初我对linq的认识只是停留在对 list<> 的泛型集合进行操作, ...

  2. DataTable、List使用groupby进行分组和分组统计;List、DataTable查询筛选方法

    DataTable分组统计: .用两层循环计算,前提条件是数据已经按分组的列排好序的. DataTable dt = new DataTable(); dt.Columns.AddRange(new ...

  3. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  4. 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码

    2013-9-15 1.    CustomValidator验证控件验证用户输入的字符长度 在实际的开发中通常会遇到验证用户输入的字符长度的问题,通常的情况下,可以写一个js的脚本或者函数,在ASP ...

  5. (转)C#用Linq实现DataTable的Group by数据统计

    本文转载自:http://www.cnblogs.com/sydeveloper/archive/2013/03/29/2988669.html 1.用两层循环计算,前提条件是数据已经按分组的列排好序 ...

  6. C# Linq及Lamda表达式实战应用之 GroupBy 分组统计

    在项目中做统计图表的时候,需要对查询出来的列表数据进行分组统计,首先想到的是避免频繁去操作数据库可以使用 Linq eg: //例如对列表中的Cu元素进行按年GroupBy分组统计 //包含年份,平均 ...

  7. Dev用于界面按选中列进行分组统计数据源(实用技巧)

    如果有用U8的可以明白这个功能就是模仿他的统计功能.我不过是把他造成通用的与适应于DEV的. (效率为6000条数据分组统计时间为3秒左右分组列过多5秒.1000条以下0.几秒,500条下0.00几秒 ...

  8. XtraGrid使用心得(折叠式主细档、分组统计)

    XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...

  9. 【转】Linq实现DataTable行列转换

    出处:http://www.cnblogs.com/li-peng/ 转换前的table: 转换后的table: 代码里有详细的说明, 还有一些参数我都截图了下面有 using System;usin ...

随机推荐

  1. html:关于表单功能的学习

    比如我在某jsp页面中写了如下表单: <form action="/MavenWeb/TestFormPost" method="get">   & ...

  2. Linux 基础入门 第一周9.14~9.20

    第一节 Linux系统简介 Linux——操作系统 1.使多个用户从不同的终端同时操作主机(分时操作系统): 2.MINIX是一个功能有限的类似于UNIX的操作系统(UNIX 实现了 TCP/IP 协 ...

  3. 解决angular2页面刷新后报404错误

    如果你的angular项目部署到一个tomcat容器里面,localhost:8080是JavaWeb的主页,localhost:8080/driver/login是你angular2项目的登陆地址. ...

  4. dom4j 使用总结

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件 dom4j的使用方法简单总结来说如下: ①可以创建一个新的xml文件 ②利用SAXReader和File对象创建一个已存在 ...

  5. JS的十大经典算法排序

    引子 有句话怎么说来着: 雷锋推倒雷峰塔,Java implements JavaScript. 当年,想凭借抱Java大腿火一把而不惜把自己名字给改了的JavaScript(原名LiveScript ...

  6. $().each() 与 $.each()解析

    在jquery 中我们可以选择$().each() 与 $.each() 进行迭代对象和数组 $(items).each(function(){ //item }) , 而后者则 $.each(ite ...

  7. Nginx下Redmine配置

    安装redmine依赖的所有ruby包 cd .. gem install bundler #注意是在网站根目录下执行 bundle install --without development tes ...

  8. Xcode的版本功能特点简要回顾

    在开始学IOS的开发时,本来是打算在windows环境下安装黑苹果的.也进行了百度和尝试,几番折腾之后,终于进入了系统界面,然而,就是然而,只有一个界面什么也动不了,后来就放弃了,咬咬牙入手了一台ma ...

  9. js获取select改变事件

    js获取select改变事件onchage前的值 和 onclick事件 <select id="wupin_id" name="wupin_id" on ...

  10. JEECMS页面中常用标签

    (1)在段落前给每一行加序号 从0开始:${a_index}从1开始:${a_index+1} (2)标记说明 [文章导航]:[@cms.Position /] [文章标题]:${arti.title ...