获取DataTable的前N行数据然后按指定字段排序
可以使用以下三种代码:
第一种:使用Linq
DataTable dtLast = dataTable.AsEnumerable().Take(count).OrderBy(dataRow => Convert.ToInt32(dataRow["Sequence"])).CopyToDataTable();
第二种:使用默认视图的Sort属性:
DataTable dtLast = dataTable.AsEnumerable().Take(count).CopyToDataTable();
dtLast.DefaultView.Sort = "Sequence asc";//逆序为Sequence desc
dtLast = dtLast.DefaultView.ToTable();
第三种:使用循环迭代
DataTable dtLast = dataTable.Clone();//克隆表结构,行数为0
for (int i = 0; i < getCount; i++)
{
dtLast.Rows.Add(dataTable.Rows[i].ItemArray);
}
//按照Sequence进行排序
DataRow[] array = dtLast.Select("1=1", "Sequence");
dtLast = array.CopyToDataTable();
整体测试程序如下:
控制台应用程序FilterDataTableDemo
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FilterDataTableDemo
{
class Program
{
static void Main(string[] args)
{
int topCount = 5;
DataTable dataTable = GetDataTable();
PrintDataTable(dataTable);
Console.WriteLine($"----------【使用Linq】打印DataTable的前【{topCount}】行并按Sequence顺序排序----------");
DataTable dtLinq = GetTopDataAndOrderUseLinq(dataTable, topCount);
PrintDataTable(dtLinq);
Console.WriteLine($"--------【使用视图View】打印DataTable的前【{topCount}】行并按Sequence顺序排序--------");
DataTable dtView = GetTopDataAndOrderUseView(dataTable, topCount);
PrintDataTable(dtView);
Console.WriteLine($"--------【使用循环迭代】打印DataTable的前【{topCount}】行并按Sequence顺序排序--------");
DataTable dtIterator = GetTopDataAndOrderUseIterator(dataTable, topCount);
PrintDataTable(dtIterator);
Console.ReadLine();
}
/// <summary>
/// 使用Linq获取前N行数据并排序
/// </summary>
/// <param name="dataTable"></param>
/// <param name="count"></param>
/// <returns></returns>
static DataTable GetTopDataAndOrderUseLinq(DataTable dataTable, int count)
{
DataTable dtLast = dataTable.AsEnumerable().Take(count).OrderBy(dataRow => Convert.ToInt32(dataRow["Sequence"])).CopyToDataTable();
return dtLast;
}
/// <summary>
/// 使用视图的默认排序
/// </summary>
/// <param name="dataTable"></param>
/// <param name="count"></param>
/// <returns></returns>
static DataTable GetTopDataAndOrderUseView(DataTable dataTable, int count)
{
DataTable dtLast = dataTable.AsEnumerable().Take(count).CopyToDataTable();
dtLast.DefaultView.Sort = "Sequence asc";//逆序为Sequence desc
dtLast = dtLast.DefaultView.ToTable();
return dtLast;
}
/// <summary>
/// 使用循环迭代获取前N行数据并排序
/// </summary>
/// <param name="dataTable"></param>
/// <param name="count"></param>
/// <returns></returns>
static DataTable GetTopDataAndOrderUseIterator(DataTable dataTable, int count)
{
//考虑到 获取的行数比数据表本身的行数还要多,这里取较小值
int getCount = Math.Min(dataTable.Rows.Count, count);
DataTable dtLast = dataTable.Clone();//克隆表结构,行数为0
for (int i = 0; i < getCount; i++)
{
dtLast.Rows.Add(dataTable.Rows[i].ItemArray);
}
//按照Sequence进行排序
DataRow[] array = dtLast.Select("1=1", "Sequence");
dtLast = array.CopyToDataTable();
return dtLast;
}
/// <summary>
/// 生成测试内存表
/// </summary>
/// <returns></returns>
static DataTable GetDataTable()
{
DataTable dt = new DataTable("Test");
dt.Columns.Add("CoreId", typeof(int));
dt.Columns.Add("Sequence", typeof(int));
dt.Columns.Add("TestName", typeof(string));
dt.Rows.Add(10, 5, "云天河");
dt.Rows.Add(9, 4, "柳梦璃");
dt.Rows.Add(8, 3, "韩菱纱");
dt.Rows.Add(7, 2, "慕容紫英");
dt.Rows.Add(6, 1, "璇玑");
dt.Rows.Add(5, 5, "怀朔");
dt.Rows.Add(4, 4, "云天青");
dt.Rows.Add(3, 3, "夙玉");
dt.Rows.Add(2, 2, "夙瑶");
dt.Rows.Add(1, 1, "玄霄");
return dt;
}
/// <summary>
/// 打印内存数据表信息
/// </summary>
/// <param name="dataTable"></param>
static void PrintDataTable(DataTable dataTable)
{
for (int i = 0; i < dataTable.Rows.Count; i++)
{
Console.WriteLine(string.Join(",", dataTable.Rows[i].ItemArray));
}
}
}
}
程序运行如图: