资源简介
C# 使用orderby 多字段 动态排序

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ConsoleApp6
{
class tmpclass
{
public int a { get; set; }
public int b { get; set; }
}
class Program
{
static void Main(string[] args)
{
List lstclass = new List();
Random rd = new Random();
for (int i = 0; i < 200000; i++) //(var item in lstclass)
{
int tmp = rd.Next(100);
int tmp2 = rd.Next(200);
lstclass.Add(new tmpclass() { a = tmp b = tmp2 });
}
Dictionary dicttmp = new Dictionary();
dicttmp.Add(“b“ SortKind.DESC);
dicttmp.Add(“a“ SortKind.ASC);
Stopwatch sw = new Stopwatch();
sw.Start();
lstclass = QuerySort.Sort(lstclass dicttmp);
sw.Stop();
StringBuilder sb = new StringBuilder();
foreach (var item in lstclass)
{
sb.AppendLine(item.a + “\t“ + item.b);
}
string tmpstr = sb.ToString();
return;
}
}
///
/// 排序方式
///
public enum SortKind
{
///
/// 升序
///
ASC = 0
///
/// 降序
///
DESC
}
///
/// 排序查询
///
public class QuerySort
{
///
/// 将实体对象集合按字段排序
///
/// 实体对象类型
/// 实体对象集合
/// 排序字段
/// 排序方式
/// 排序后的集合
public static List Sort(List list string field SortKind kind)
{
if (list != null)
{
switch (kind)
{
case SortKind.ASC: list = list.OrderBy(t => GetValue(t field)).ToList(); break;
case SortKind.DESC: list = list.OrderByDescending(t => GetValue(t field)).ToList(); break;
}
}
return list;
}
public static List Sort(List list Dictionary dicsort)
{
if (list != null)
{
IOrderedEnumerable tmporderlist = null;
foreach (var item in dicsort)
{
switch (item.Value)
{
case SortKind.ASC:
{
if (tmporderlist == null)
{
tmporderlist = list.OrderBy(t => GetValue(t ite
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5109 2019-07-01 14:36 Program.cs
相关资源
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
评论
共有 条评论