Linq实现DataTable的分组统计
2010-07-06 00:36
507 查看
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的分组统计
- C# Linq To DataTable 分组统计 DEMO
- C# Linq To DataTable 分组统计
- 使用LINQ实现分组统计
- Linq对DataTable或者集合的排序,Where筛选,分组,统计总数sum等操作
- LINQ实现多次调用GroupBy进行分组统计
- Linq对DataTable或者集合的排序,Where筛选,分组,统计总数sum等操作
- 用linq做datatable分组统计时,字段为null 转换类型出错的解决方案 亲测
- C# Linq To DataTable 分组统计
- Linq对DataTable数据的分组统计
- 实现按部门月卡余额总额分组统计的SQL查询代码
- DataTable实现数据统计
- Linq实践教程(1):一行代码实现DataTable全文搜索(Full Text Search)
- Linq实践系列(1):一行代码实现DataTable全文搜索(Full Text Search)
- 【案例分享】电力设备生产数据的多层分组统计报表实现
- Linq DataTable Group By 分组显示人员明细
- 利用bobo-browse 实现lucene的分组统计功能
- DataTable、List使用groupby进行分组和分组统计;List、DataTable查询筛选方法
- 利用 GROUP BY 和 MAX 实现对表数据分组统计后的过滤查询(Oracle920)