使用linq的OrderBy,如果明确知道是哪个字段,当然很容易:

 IQueryable<User> userQuery = ...;
userQuery.OrderBy(u => u.Code)

但假如我们想写一个通用方法,预先并不知道要用哪个字段排序呢?

在网上寻寻觅觅,有许多国内的博客互相抄袭,信誓旦旦,但其实那些代码都运行不了。

还是老外的好使:

http://www.4byte.cn/question/33782/dynamic-orderby-using-linq-dynamic.html

我拿来修改了一下,可以用。主要思想是扩展Queryable。但里面的东西有许多我都看不懂。

    public static class QueryableExtension
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName)
{
return _OrderBy<T>(query, propertyName, false);
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> query, string propertyName)
{
return _OrderBy<T>(query, propertyName, true);
} static IOrderedQueryable<T> _OrderBy<T>(IQueryable<T> query, string propertyName,bool isDesc)
{
string methodname = (isDesc) ? "OrderByDescendingInternal" : "OrderByInternal"; var memberProp = typeof(T).GetProperty(propertyName); var method = typeof(QueryableExtension).GetMethod(methodname)
.MakeGenericMethod(typeof(T), memberProp.PropertyType); return (IOrderedQueryable<T>)method.Invoke(null, new object[] { query, memberProp });
}
public static IOrderedQueryable<T> OrderByInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty)
{//public
return query.OrderBy(_GetLamba<T, TProp>(memberProperty));
}
public static IOrderedQueryable<T> OrderByDescendingInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty)
{//public
return query.OrderByDescending(_GetLamba<T, TProp>(memberProperty));
}
static Expression<Func<T, TProp>> _GetLamba<T, TProp>(PropertyInfo memberProperty)
{
if (memberProperty.PropertyType != typeof(TProp)) throw new Exception(); var thisArg = Expression.Parameter(typeof(T));
var lamba = Expression.Lambda<Func<T, TProp>>(Expression.Property(thisArg, memberProperty), thisArg); return lamba;
}
}

调用:

IQueryable<User> userQuery = ...;
//正序
userQuery = userQuery.OrderBy("Code");
//降序
userQuery = userQuery.OrderByDescending("Code");

让Linq的OrderBy支持动态字段的更多相关文章

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

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

  2. List使用linq的OrderBy方法排序,并按照两个字段排序的写法

    SfaMember.GetList(searchInfo, 0, 1000, out Allcount).Where(item => item.bOpen == true).OrderBy(it ...

  3. 写一个针对IQueryable<T>的扩展方法支持动态排序

    所谓的动态排序是指支持任意字段.任意升序降序的排序.我们希望在客户端按如下格式写: localhost:8000/api/items?sort=titlelocalhost:8000/api/item ...

  4. Linq技术四:动态Linq技术 -- Linq.Expressions

    前面介绍了Linq的三个方面应用:Linq to SQL, Linq to XML和Linq to Object,这篇介绍一下动态Linq的实现方式及应用场景. 命名空间: System.Linq; ...

  5. 如何让帝国CMS7.2搜索模板支持动态标签调用

    帝国cms站内搜索一般不支持动态标签调用,如果要调用如何实现呢?修改两个地方就可以实现了.打开 /e/search/result/index.php 文件,找到(文件改了,不会调用也是徒劳!看看这个帝 ...

  6. 转载LINQ系列OrderBy(), ThenBy()简介

    前言 前面两篇分别介绍了 Where() 与 Select() ,这篇则是要介绍 OrderBy() 与 ThenBy() ,这几个东西看起来最像 SQL 上会用到的语法,但切记一点,这边介绍的是 L ...

  7. 帝国cms7.0,列表模板调用不支持附表字段

    帝国cms在制作列表模板时,是不支持一些字段的调用的,原因是因为有些字段所在的位置为附表,本段详细向你介绍 帝国如何调用副表字段 我们可在 系统---管理数据表---管理字段中查看 如果我们需要调用附 ...

  8. mybatis操作动态表+动态字段+存储过程

    存储过程 statementType="CALLABLE" <!-- 计算金额存储过程--> <update id="getCalcDistributo ...

  9. Omi应用md2site-0.5.0发布-支持动态markdown拉取解析

    写在前面 Md2site是基于Omi的一款Markdown转网站工具,使用简单,生成的文件轻巧,功能强大. 官网:http://alloyteam.github.io/omi/md2site/ Git ...

随机推荐

  1. spring-mvc junit测试

    import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; impor ...

  2. 条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)

    NOTE: 1.复制RAII 对象必须一并赋值它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍而常见的RAII class copying 行为是: 抑制c ...

  3. 第四讲:debugging simulation mismatches

    关于竞争冒险: 1.use +race utility to locate race condition code **** 2.use $vcdplusdeltacycleon to locate ...

  4. 使用github中py12306抢票系得

    首先需要安装最新的python:安装步骤见:https://www.cnblogs.com/weven/p/7252917.html 其次下载python源码: 链接:https://pan.baid ...

  5. [第一波模拟\day3\T3]{益智游戏}(game.cpp)

    [问题描述] 小P和小R在玩一款益智游戏.游戏在一个正权有向图上进行. 小P控制的角色要从A点走最短路到B点,小R控制的角色要从C点走最短路到D点. 一个玩家每回合可以有两种选择,移动到一个相邻节点或 ...

  6. 会跳舞的树(只用HTML+CSS)(转)

    效果如下. 共有1022个<div>元素. See the Pen wKvrMa by moyu (@MoYu1991) on CodePen.

  7. maven+Spring环境搭建

    一,项目结构图 二,applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> &l ...

  8. JSP中使用<c:forEach>标签循环遍历元素

    转载:http://blog.csdn.net/hero_cheng/article/details/51924577

  9. php 面向对象 (类 对象)

    //面向对象//什么是面向对象//面向过程//什么是对象?//一切皆是对象//类//由对象抽象化//造类//class Ren//{ //构造方法 - - 写不写都存在//类的初始化方法 //构造方法 ...

  10. 使用cURL和用户名和密码?

    问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...