之前网上搜索的相关方法都是使用了反射的方法来动态获取字段,以实现动态linq排序,但是因为项目组觉得此方法效率低下,所以不予采纳。

所以有了以下代码

 public interface IBase{
dynamic GetField(string field);
} public class Employee : IBase
{
public int ID { get; set; }
public string FName { get; set; }
public int Age { get; set; }
public char Sex { get; set; } public dynamic GetField(string field)
{
switch (field.ToUpper()) {
case "ID":
return ID;
case "FNAME":
return FName;
case "AGE":
return Age;
case "SEX":
return Sex;
default:
return ID;
}
}
}

  这是实体类,缺点是必须实现接口方法GetField,但这也是重点。

以下是对应的排序方法

/// <summary>
/// 对结果集进行排序
/// </summary>
/// <param name="source">结果集</param>
/// <param name="dict">参数列表KEY:OrderBy Ascending VALUE:以,分隔</param>
/// <returns></returns>
protected List<IBase> OrderBy(List<IBase> source, Dictionary<string, string> dict)
{
if (dict.ContainsKey("OrderBy"))
{
try
{
string[] Order = dict["OrderBy"].Split(',');
string[] ascend = dict.ContainsKey("Ascending") ? dict["Ascending"].Split(',') : new string[] { "1" };
IOrderedEnumerable<IBase> current = null;
if (ascend[0] == "1")
current = source.OrderBy(p => p.GetField(Order[0]));
else
current = source.OrderByDescending(p => p.GetField(Order[0])); int index = 0;
for (int i = 1; i < Order.Length; i++)
{
index = ascend.Length > i ? i : ascend.Length - 1;
if (ascend[index] == "1")
current = current.ThenBy(p => p.GetField(Order[i]));
else
current = current.ThenByDescending(p => p.GetField(Order[i]));
} return current.ToList();
}
catch { }
}
return source;
}

  以下是测试方法

public void LinqOrder()
{
var empList = new List<Employee>();
Random r = new Random();
int length = 10000000; Console.WriteLine(string.Format("开始装载数据({0})...", length));
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < length; i++)
{
empList.Add(new Employee()
{
ID = i,
FName = "A" + i,
Age = r.Next(0, 100),
Sex = r.Next(0, 1) == 1 ? 'F' : 'M'
});
}
sw.Stop();
Console.WriteLine(string.Format("{0}条数据装载完成,时间为:{1}", length, sw.ElapsedMilliseconds)); Console.WriteLine("开始转换数据,Employee to IBase");
sw = new Stopwatch();
sw.Start();
var list = empList.ToList<IBase>();
sw.Stop();
Console.WriteLine(string.Format("{0}条数据转换,Employee to IBase,时间为:{1}", length, sw.ElapsedMilliseconds)); Dictionary<string, string> dict = new Dictionary<string, string>();
dict["OrderBy"] = "Age,Sex";
dict["Ascending"] = "1,1";
Console.WriteLine("开始排序");
sw = new Stopwatch();
sw.Start();
OrderBy(list, dict);
sw.Stop();
Console.WriteLine(string.Format("{0}条数据使用dynamic排序时间为:{1}", length, sw.ElapsedMilliseconds)); Console.WriteLine("开始普通排序");
sw = new Stopwatch();
sw.Start();
empList.OrderBy(p => p.Age).ThenBy(p => p.Sex).ToList();
sw.Stop();
Console.WriteLine(string.Format("{0}条数据使用字段排序时间为:{1}", length, sw.ElapsedMilliseconds));
}

  以下是测试结果

linq 动态排序,不使用反射的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(17)-LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为s ...

  2. LinQ动态排序

    LinQ动态排序 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为sort这个名称填写错误,会导致后台一直无法获取datagrid的排序字段 本来是没有这一讲的,为了使2 ...

  3. ASP.NET MVC5+EF6+EasyUI 后台管理系统(17)-LinQ动态排序

    系列目录 首先修复程序中的一个BUG这个BUG在GridPager类中,把sord修改为sort这个名称填写错误,会导致后台一直无法获取datagrid的排序字段 本来是没有这一讲的,为了使20行的代 ...

  4. 使用Linq动态排序

    Linq排序很方便,如果能动态创建Expression再排序就更方便了. 正序还是倒序排列 var order = typeof(Enumerable).GetMember(direction == ...

  5. MVC Linq动态排序

    在nuget 中searh System.Linq.Dynamic 安装对应的版本, 这样都可以使用了 var orderExpression = string.Format("{0} {1 ...

  6. linq 动态排序 order by

    项目查询数据库使用的是linq 语法,可是后期需要用到不同字段的排序.各种纠结! 在网上找了各种资料 后面才找到两种方法 using System; using System.Collections. ...

  7. linq 动态排序

    /// <summary> /// 排序 /// </summary> /// <typeparam name="T"></typepar ...

  8. Linq 动态查询排序

    Linq的排序一般是这样写的: query.OrderBy(x => x.Tel).Skip().Take(); 实际使用中排序字段可能是通过字符类型的参数来设置的,于是想这样实现: query ...

  9. linq扩展之动态排序

    前两天看QQ群里面,一位朋友问的问题,说在linq中怎么实现动态排序呢,自己想了半天,没有头绪,网上找了下相关的资料,看了下,收益挺多,记录下来. 之前我们没有如果不知道动态排序的方法的话,我们可能会 ...

随机推荐

  1. Android 一步一步教你使用ViewDragHelper

    在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...

  2. javac 命令用法

    引用自己写的Class 在java中手动编译时,总提示找不到类,调试成功后,特把目录结构与编译成功的命令列出: 样例一: 文件名 MessageStore.java Hello.java 源码 pac ...

  3. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.2.Oracleasm Createdisk ASM磁盘失败:Instantiating disk: failed

    1.错误信息:Instantiating disk: failed [root@linuxrac1 /]# /usr/sbin/oracleasm createdisk OCR_VOTE /dev/s ...

  4. 看看JavaScript中void(0)的含义

    JavaScript中void是一个操作符,该操作符指定要计算一个表达式但是不返回值. void 操作符用法格式如下:1. javascript:void (expression)2. javascr ...

  5. 微信公众平台开发localStorage数据总是被清空

    我把现在项目中的用户数据存储过程改成本地的,只用localStorage,但是随之而来很多问题,原因就是localStorage只有很短的有效时间,退出公众号,关闭微信都会清空.最不能容忍的是用户还在 ...

  6. just test Gson

    just test Gson code package com.qilin.test; import com.google.gson.Gson; import org.apache.commons.l ...

  7. CentOS7 安装 scala 2.11.1

    wget http://downloads.typesafe.com/scala/2.11.6/scala-2.11.6.tgz?_ga=1.61986863.2013247204.144801902 ...

  8. DOM笔记(五):JavaScript的常见事件和Ajax小结

    一.常见事件类型 1.鼠标事件 事件名称 说明 onclick 鼠标单击时触发 ondbclick 鼠标双击时触发 onmousedown 鼠标左键按下时触发 onmouseup 鼠标释放时触发 on ...

  9. Compiling Xen-4.4 From Source And Installing It On Ubuntu Server (Amd-64)

    First of all, you should install a clean Ubuntu Server (Amd-64) on your server. (Version 14.04 is st ...

  10. HDOJ-ACM1009(JAVA) (传说中的贪心算法)分为数组实现 和 封装类实现

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5564311.html 这个道题有几点要注意的: 数组存放的类型:float或double 打印的格式:(如果只是 ...