groupList是原始数据集合,List<T>

sortOrder是排序类型,desc 或者asc

sortName是排序属性名称

1.使用反射。

private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
} var resultList = sortOrder == "desc" ? groupList.OrderByDescending(p => GetPropertyValue(p, sortName)) : groupList.OrderBy(p => GetPropertyValue(p, sortName)); //linq方式:
//
var resultList1 = from p in groupList orderby GetPropertyValue(p, m.SortName) select p; if (sortOrder == "desc")
resultList1 = from p in groupList orderby GetPropertyValue(p, sortName) descending select p;

2.调用AsQueryable()

将泛型 System.Collections.Generic.IEnumerable<T> 转换为泛型 System.Linq.IQueryable<T>。

var groupQueryList = groupList.AsQueryable();//here
var tmpList = groupQueryList.OrderBy(sortName, sortOrder);

需要如下扩展方法:

    public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName) {
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach(string prop in props) {
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg); object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length ==
&& method.GetParameters().Length == )
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] {source, lambda});
return (IOrderedQueryable<T>)result;
}

参考:http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet#41262

C# linq查询 动态OrderBy的更多相关文章

  1. LINQ查询表达式---------orderby子句

    LINQ查询表达式---------orderby子句 LINQ可以按元素的一个或多个属性对元素进行排序. class Program { public class PerInfo { public ...

  2. linq 实现动态 orderby

    class Pet { public string Name{get;set;} public int Age{get;set;} } void Main() { Pet[] pets = { }, ...

  3. 使用Expression Tree构建动态LINQ查询

    这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...

  4. .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending

    .NET 3.5(5) - LINQ查询操作符之Select.Where.OrderBy.OrderByDescending 介绍    ·Select - Select选择:延迟    ·Where ...

  5. LINQ 如何动态创建 Where 子查询

    还是那句话,十年河东,十年河西,莫欺少年穷! 学无止境,精益求精... 今天探讨下如何构造动态的LINQ子查询 LINQ,相信大家都写过,很简单,下面以一个基本的范例说明下: namespace Co ...

  6. Linq 支持动态字查询集合, 也就是说根据传入的值进行查询。

    Linq 支持动态字查询集合, 也就是说根据传入的值进行查询. 比如我们有个类Patient, 其中有个字段PatientName, 现在有Patient集合, 想要查询PatientName为&qu ...

  7. [C#.NET 拾遗补漏]13:动态构建LINQ查询表达式

    最近工作中遇到一个这样的需求:在某个列表查询功能中,可以选择某个数字列(如商品单价.当天销售额.当月销售额等),再选择 小于或等于 和 大于或等于 ,再填写一个待比较的数值,对数据进行查询过滤. 如果 ...

  8. Linq查询表达式

    目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...

  9. Linq学习之旅——LINQ查询表达式

    1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. 概述 ...

随机推荐

  1. ionic的加载功能

    下面是代码(黄色背景的是加载功能的代码): <html ng-app="ionicApp"> <head> <meta charset="u ...

  2. javaWeb注册,登陆,注销功能的实现

    一:注册页面:regist.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8& ...

  3. MySQL 官方文档

    MySQL 5.6 Reference Manual Preface and Legal Notices 1 General Information 2 Installing and Upgradin ...

  4. 学习使用Jmeter做压力測试(一)--压力測试基本概念

    一.性能測试的概念         性能測试是通过自己主动化的測试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行測试.负载測试和压力測试都属于性能測试,两者能够结合进行. 通过负载測试, ...

  5. 在 VirtualBox 虚拟机中安装 Arch Linux 系统指南

    How to install Arch Linux on VirtualBox 在 VirtualBox 虚拟机中安装 Arch Linux 系统指南 本文导航 1.Arch Linux 软件仓库2. ...

  6. nginx配置用户认证

    location ~ .*admin\.php$ {             auth_basic "weifenglinux auth";             auth_ba ...

  7. PHP-WebService中Endpoint、Disco、WSDL都是做什么的?

    Endpoint: http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx   web服务的URI地址,你访问之后,就会出现web服务的相 ...

  8. pageEncoding和ContextType区别

    http://blog.csdn.net/kerrywang/article/details/4454895 pageEncoding        在JSP标准的语法中,如果 pageEncodin ...

  9. LaunchScreen.storyboard 设置图片后不显示(转)

    LaunchScreen.storyboard 设置图片后不显示 将图片放在根目录下即可 3D85E99F-A79B-4419-817D-1417E1446624.png   转至:http://ww ...

  10. Redis总结(二)C#中如何使用redis(转载)

    上一篇讲述了安装redis<Redis总结(一)Redis安装>,同时也大致介绍了redis的优势和应用场景.本篇着重讲解.NET中如何使用redis和C#. Redis官网提供了很多开源 ...