C#对DataTable中的某列进行分组
作者:.NET开发菜鸟 时间:2021-12-10 23:06:45
有时候我们从数据库中查询出来数据之后,需要按照DataTable的某列进行分组,可以使用下面的方法实现,代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataTableGroupDemo
{
class Program
{
static void Main(string[] args)
{
// 准备数据
DataTable dt = new DataTable();
// 创建列
DataColumn dcName = new DataColumn("Name", typeof(string));
DataColumn dcAge = new DataColumn("Age", typeof(Int32));
DataColumn dcScore = new DataColumn("Score", typeof(Int32));
// 添加列
dt.Columns.Add(dcName);
dt.Columns.Add(dcAge);
dt.Columns.Add(dcScore);
// 添加数据
dt.Rows.Add(new object[] { "Tom", 23, 67 });
dt.Rows.Add(new object[] { "Tom", 23, 67 });
dt.Rows.Add(new object[] { "Jack", 21, 100 });
dt.Rows.Add(new object[] { "Greey", 24, 56 });
dt.Rows.Add(new object[] { "Kevin", 24, 77 });
dt.Rows.Add(new object[] { "Tom", 23, 82 });
dt.Rows.Add(new object[] { "Greey", 24, 80 });
dt.Rows.Add(new object[] { "Jack", 21, 90 });
#region 使用Linq expression to DataTable group by
var query = from p in dt.AsEnumerable()
group p by new { name = p.Field<string>("Name"),score=p.Field<Int32>("Score") } into m
select new
{
Name = m.Key.name,
Score=m.Key.score
};
#endregion
// 输出
Console.WriteLine("Linq");
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.WriteLine("GroupBy");
IEnumerable<IGrouping<string, DataRow>> result = dt.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["Name"].ToString());
foreach (IGrouping<string, DataRow> ig in result)
{
Console.WriteLine("key=" + ig.Key + ":");
foreach (DataRow dr in ig)
{
Console.WriteLine(dr["Name"].ToString().PadRight(10) + dr["Age"].ToString().PadRight(10) + dr["Score"].ToString().PadRight(10));
}
}
Console.ReadKey();
}
}
}
程序运行效果
来源:https://www.cnblogs.com/dotnet261010/p/11495884.html
标签:C#,DataTable,分组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 调试工具用法详细介绍
2022-07-18 21:34:05
Springboot整合knife4j与shiro的操作
2023-08-25 08:31:42
![](https://img.aspxhome.com/file/2023/0/76280_0s.png)
Android布局之帧布局FrameLayout详解
2023-08-07 04:45:29
![](https://img.aspxhome.com/file/2023/5/85985_0s.png)
spring boot RestTemplate 发送get请求的踩坑及解决
2022-01-19 16:08:09
![](https://img.aspxhome.com/file/2023/2/69662_0s.png)
Android 处理OnItemClickListener时关于焦点颜色的设置问题
2021-09-02 01:47:00
![](https://img.aspxhome.com/file/2023/0/94980_0s.png)
springboot对压缩请求的处理方法
2022-02-11 14:00:59
Java设计模式之状态模式State Pattern详解
2023-11-07 07:55:01
![](https://img.aspxhome.com/file/2023/7/80317_0s.png)
在多线程中调用winform窗体控件的实现方法
2023-09-13 09:07:43
Java线程中的常见方法(start方法和run方法)
2023-11-16 17:41:32
![](https://img.aspxhome.com/file/2023/4/68694_0s.png)
JSON.toJSONString()空字段不忽略修改的问题
2023-06-16 03:12:37
javaweb中Http协议详解
2022-03-21 05:12:41
![](https://img.aspxhome.com/file/2023/8/82998_0s.png)
C#嵌套类的访问方法
2021-07-26 18:03:20
C#开发WinForm清空DataGridView控件绑定的数据
2022-12-01 11:22:08
![](https://img.aspxhome.com/file/2023/7/91327_0s.jpg)
Java实现飞机航班管理系统的思路详解
2022-08-20 19:48:42
![](https://img.aspxhome.com/file/2023/6/78386_0s.jpg)
Unity3D自定义创建圆锥体
2022-05-04 10:12:34
![](https://img.aspxhome.com/file/2023/4/88354_0s.png)
Java实现LeetCode(54.螺旋矩阵)
2023-01-26 20:54:00
SpringCloud网关组件zuul实例解析
2023-04-13 08:51:47
![](https://img.aspxhome.com/file/2023/1/62971_0s.png)
springboot中的springSession的存储和获取实现
2023-11-04 10:23:14
java对象拷贝详解及实例
2023-01-20 08:27:00
Java try()语句实现try-with-resources异常管理机制操作
2022-02-06 18:29:16
![](https://img.aspxhome.com/file/2023/1/78961_0s.png)