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
- NotePad++ 显示字符
转: http://shouce.jb51.net/notepad_book/npp_func_show_special_char.html
- (转)Behavior Tree实践
http://www.cnblogs.com/mavaL/archive/2013/04/07/3001860.html
- 【python】下载网络文件到本地
# 下载网络图片文件到本地 import urllib.request rsp=urllib.request.urlopen("http://n.sinaimg.cn/ent/transfo ...
- centos下docker网络桥接
停止服务 停止docker0网卡 Ip link set dev docker0 down 删除docker0 Brctl delbr docker0 进入到网卡的配置文件创建桥接网络br0 Brct ...
- 谋哥:《App自推广》开篇之回到远古人类
[谋哥每天一干货.第六十八篇] 这两天帮谋天团的杨整体验他的App--"闪聊"的内測新版,改版后这款App命名为"美丫",一款致力于打造国内首款专注于女性社交的 ...
- 【Hibernate一】概述及入门
Hibernate学习框架: 1.基本部分 crud的操作 主键的生成机制 类型 持久化类 映射文件 *.hbm.xml 配置文件 hibe ...
- C#基础视频教程7.2 如何编写简单游戏
前面一小节我们实现了简单的碰撞检测,但是实际上游戏的对象并不是一个标准的矩形(小鸟是一个不规则的物体,其实碰撞的管道也是不规则物体),所以如果真的要做的比较完美,我们自己要写一个方法,能够导入一个图像 ...
- Could not find RubyGem cocoapods 错误
之前安装过一次cocoapods 后来,安装octopress,安装过一次较新版的ruby,可能是由于ruby安装,把之前的cocoapods删除了,结果使用,出现如图错误 解决: 重新安装 coco ...
- STL之hashtable源代码剖析
// Filename: stl_hashtable.h /////////////////////////////////////////////////////////////////////// ...