C# linq查询 动态OrderBy
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的更多相关文章
- LINQ查询表达式---------orderby子句
LINQ查询表达式---------orderby子句 LINQ可以按元素的一个或多个属性对元素进行排序. class Program { public class PerInfo { public ...
- linq 实现动态 orderby
class Pet { public string Name{get;set;} public int Age{get;set;} } void Main() { Pet[] pets = { }, ...
- 使用Expression Tree构建动态LINQ查询
这篇文章介绍一个有意思的话题,也是经常被人问到的:如何构建动态LINQ查询?所谓动态,主要的意思在于查询的条件可以随机组合,动态添加,而不是固定的写法.这个在很多系统开发过程中是非常有用的. 我这里给 ...
- .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending
.NET 3.5(5) - LINQ查询操作符之Select.Where.OrderBy.OrderByDescending 介绍 ·Select - Select选择:延迟 ·Where ...
- LINQ 如何动态创建 Where 子查询
还是那句话,十年河东,十年河西,莫欺少年穷! 学无止境,精益求精... 今天探讨下如何构造动态的LINQ子查询 LINQ,相信大家都写过,很简单,下面以一个基本的范例说明下: namespace Co ...
- Linq 支持动态字查询集合, 也就是说根据传入的值进行查询。
Linq 支持动态字查询集合, 也就是说根据传入的值进行查询. 比如我们有个类Patient, 其中有个字段PatientName, 现在有Patient集合, 想要查询PatientName为&qu ...
- [C#.NET 拾遗补漏]13:动态构建LINQ查询表达式
最近工作中遇到一个这样的需求:在某个列表查询功能中,可以选择某个数字列(如商品单价.当天销售额.当月销售额等),再选择 小于或等于 和 大于或等于 ,再填写一个待比较的数值,对数据进行查询过滤. 如果 ...
- Linq查询表达式
目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...
- Linq学习之旅——LINQ查询表达式
1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. 概述 ...
随机推荐
- JVisualVM简介与内存泄漏实战分析
JVisualVM简介与内存泄漏实战分析 学习了:https://blog.csdn.net/kl28978113/article/details/53817827
- 10分钟,利用canvas画一个小的loading界面
首先利用定义下canvas得样式 <canvas width="1024" height="720" id="canvas" styl ...
- Oracle 检索数据
SELECT * | { [ DISTINCT ] column | expression [ alias ] , ... } FROM ta ...
- JUnit 3.8 演示递归删除文件目录的 测试类程序 .
用递归方式来实现删除硬盘的文件或目录(空文件夹) 首先要找到递归的入口及出口,这点很重要,成败在此,呵呵! 代码实现: import java.io.File ; class RecursionDel ...
- 【五年】Java打怪升级之路
之前写过一篇帖子.就是关于工作经验分享的,近期非常多人私信我.所以博客这边再分享一次 这几年来,我最大的感想就是一句话:多看.多写.多想.多问.多分享.多优化.多运动... 1.[多看] 读万卷书,行 ...
- oracle 三表关联查询
oracle 三表关联查询 CreationTime--2018年7月4日17点52分 Author:Marydon 左连接实现三表关联 表A--------------------------- ...
- 【BIEE】安装好BIEE后,修改默认登录页面不为QuickStart页面
已经安装好了BIEE,但是发布了自己的资料库后,默认的登录页面为QuickStart,导致已登录就看到错误页面 现在进行如下修改即可 点击登录身份后的名字,例如我的是weblogic 选择[我的账户] ...
- HQL的select new map ···语法
通常hibernate查询出的结果集是类似于 List<T> 或 List<Object[]> 的类型 类似于下面这个方法 public List<SfJmsfT> ...
- 关于python 中的 sys.argv 的使用方法
sys.argv是获取在cmd运行python文件的时候输入的命令行参数,呈现的数据结构是列表的格式 1.用pacharm时运行时的结果是: 输出结果: 2.当我在cmd中输入指令 debu ...
- 怎么使用Firefox的RestClient