AI智能
改变未来

(原创)[C#] DataTable排序扩展方法


一,前言

DataTable的应用极其广泛,对DataTable进行排序也有很多方式,每种的实现方式都不难,但是使用起来却比较繁琐,所以本人便写了一个扩展方法,专门对DataTable进行操作。本篇是使用Linq的方式去实现排序,代码很简单,封装后,使用起来也极其方便。本扩展方法支持升序/降序排列,支持列以String、Double、Int、Datetime等方式排序

相信看完的你,一定会有所收获!

本文地址:https://www.cnblogs.com/lesliexin/p/15212026.html

二,源码

下面直接贴出扩展方法源代码,代码很简单:

using System;using System.Data;using System.Linq;namespace DatatableSortDemo{/// <summary>/// DataTable排序扩展/// </summary>public static class DatatableSort{/// <summary>/// 排序方式/// </summary>public enum SortType{/// <summary>/// 升序/// </summary>ASC,/// <summary>/// 降序/// </summary>DESC}/// <summary>/// 列的类型/// </summary>public enum ColumnType{/// <summary>/// String/// </summary>STRING,/// <summary>/// Int/// </summary>INT,/// <summary>/// Double/// </summary>DOUBLE,/// <summary>/// Datetime/// </summary>DATETIME}/// <summary>/// String转Double/// </summary>/// <param name="str"></param>/// <returns></returns>public static double StrToDouble(this string str){try{var d = Convert.ToDouble(str);return d;}catch (Exception){return 0;}}/// <summary>/// String转Int/// </summary>/// <param name="str"></param>/// <returns></returns>public static int StrToInt(this string str){try{var d = str.StrToDouble();int i = Convert.ToInt32(d);return i;}catch (Exception){return 0;}}/// <summary>/// String转DateTime/// </summary>/// <param name="str"></param>/// <returns></returns>public static DateTime StrToDatetime(this string str){try{var d = Convert.ToDateTime(str);return d;}catch (Exception){return new DateTime();}}/// <summary>/// String转Object/// </summary>/// <param name="str"></param>/// <param name="colType"></param>/// <returns></returns>public static object StrToObject(this string str,ColumnType colType){if (colType == ColumnType.STRING){return str;}else if (colType == ColumnType.DOUBLE){return str.StrToDouble();}else if (colType == ColumnType.INT){return str.StrToInt();}else{return str.StrToDatetime();}}/// <summary>/// 排序/// </summary>/// <param name="dataTable">待排序Datatable</param>/// <param name="colName">排序的列</param>/// <param name="colType">排序列的类型</param>/// <param name="sortType">排序方式</param>/// <returns>排序后的Datetable</returns>public static DataTable Sort(this DataTable dataTable, string colName, ColumnType colType, SortType sortType){try{if (sortType == SortType.ASC){var dt = dataTable.Rows.Cast<DataRow>().OrderBy(r => r[colName].ToString().StrToObject(colType)).CopyToDataTable();return dt;}else{var dt = dataTable.Rows.Cast<DataRow>().OrderByDescending(r => r[colName].ToString().StrToObject(colType)).CopyToDataTable();return dt;}}catch (Exception){return null;}}}}

三,使用示例

将上面的源码添加到项目中,然后在DataTable后使用即可。

using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;namespace DatatableSortDemo{class Program{static void Main(string[] args){DataTable dt = new DataTable();dt.Columns.Add(new DataColumn("A"));dt.Columns.Add(new DataColumn("B"));dt.Columns.Add(new DataColumn("C"));dt.Columns.Add(new DataColumn("D"));dt.Rows.Add("字符串3", "2021-08-31 10:00:00", "1", "45.6");dt.Rows.Add("字符串2", "2020-08-31 10:00:00", "2", "23.7");dt.Rows.Add("字符串1", "2019-08-31 10:00:00", "3", "99");dt.Rows.Add("字符串4", "2021-08-31 11:00:00", "4", "12");dt.Rows.Add("字符串5", "2021-08-31 10:01:00", "5", "34.5");var d1 = dt.Sort("A", DatatableSort.ColumnType.STRING, DatatableSort.SortType.ASC);var d1_1 = dt.Sort("A", DatatableSort.ColumnType.STRING, DatatableSort.SortType.DESC);var d2 = dt.Sort("B", DatatableSort.ColumnType.DATETIME, DatatableSort.SortType.ASC);var d2_1 = dt.Sort("B", DatatableSort.ColumnType.DATETIME, DatatableSort.SortType.DESC);var d3 = dt.Sort("C", DatatableSort.ColumnType.INT, DatatableSort.SortType.ASC);var d3_1 = dt.Sort("C", DatatableSort.ColumnType.INT, DatatableSort.SortType.DESC);var d4 = dt.Sort("D", DatatableSort.ColumnType.DOUBLE, DatatableSort.SortType.ASC);var d4_1 = dt.Sort("D", DatatableSort.ColumnType.DOUBLE, DatatableSort.SortType.DESC);Console.WriteLine("按列A正序:");DatatablePrint(d1);Console.WriteLine("按列A倒序:");DatatablePrint(d1_1);Console.WriteLine("--------------------");Console.WriteLine("按列B正序:");DatatablePrint(d2);Console.WriteLine("按列B倒序:");DatatablePrint(d2_1);Console.WriteLine("--------------------");Console.WriteLine("按列C正序:");DatatablePrint(d3);Console.WriteLine("按列C倒序:");DatatablePrint(d3_1);Console.WriteLine("--------------------");Console.WriteLine("按列D正序:");DatatablePrint(d4);Console.WriteLine("按列D倒序:");DatatablePrint(d4_1);Console.WriteLine("--------------------");Console.ReadKey();}static void DatatablePrint(DataTable dt){string s = "";int iColCount = dt.Columns.Count;foreach( DataColumn col in dt.Columns){s += col.ColumnName + "\\t";}Console.WriteLine(s);foreach (DataRow row in dt.Rows){s = "";for(int i=0;i< dt.Columns.Count; i++){s += row[i].ToString() + "\\t";}Console.WriteLine(s);}}}}

四,运行结果

运行结果如下,排序都正确的。

-【END】-

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » (原创)[C#] DataTable排序扩展方法