使用Expression做Linq的參數化排序
Linq非常的好用,減少大量的資料庫操作手序,使用具名的類別,減少了在程式中寫SQL寫錯字的可能性,問題來了,如果我想用QueryString中的參數,作為排序的依據,但是因為是具名的類別,不能指定字串,剛開始我是用switch一個一個指定,但欄位一多就覺得這方法很笨,在搜尋更好的方法中發現使用System.Linq.Expressions.Expression可以決解這個問題。 如果各位有仔細看,會發現System.Linq.Queryable下的Method參數都有Expression,如本篇要用的OrderBy。
1
2
3
4
|
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>( this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector); public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>( this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer); public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>( this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector); public static IOrderedQueryable<TSource> OrderByDescending<TSource, TKey>( this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, IComparer<TKey> comparer); |
那什麼是Expression呢?
簡單說是動態產生Delegate,真的執行的時候才Compile,你會說在Visaul Studio明明就Compile啦,怎麼又到執行才Compile?
實際上在Visaul Studio Compile時,會先編成Expression,等到執行時再將Expression Compile成Delegate,如下面範例。
1
2
3
4
5
6
|
//在Visual Studio這樣寫的東西。 .OrderBy(x=>x.Name); //Compile後其實是編成Expression(從.Net Reflector中取得後,有修改成易讀格式)。 Parameter p = Expression.Parameter( typeof (type), "x" ); // 參數X Expression.Lambda<Func<type, string >>(Expression.Property(p, "Name" ), p)); // x.Name//並不是所有的Lambda都會編成Expression,而是只有參數是Expression才會編成Expression,其他的還是直接編成Method。 |
EXPRESSION TREES, TAKE TWO – INTRODUCING SYSTEM.LINQ.EXPRESSIONS V4.0
Building LINQ Queries at Runtime in C#
所以也可以自己產生Expression做OrderBy的參數,範例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
if (! string .IsNullOrEmpty( this .Request.QueryString[ "Order" ])){ // 產生Expression var param = Expression.Parameter( typeof (Project), "x" ); var orderExpression = Expression.Lambda<Func<Project, object >>(Expression.Property(param, this .Request.QueryString[ "Order" ]), param); if ( this .Request.QueryString[ "OrderDirection" ] == "Desc" ) { query = query.OrderByDescending(orderExpression); } else { query = query.OrderBy(orderExpression); } } |
但上面的範例遇到遇到nullable的型別會掛到所以又寫了幾個Extension來使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public static class IQueryableExtension { private static MethodInfo orderbyInfo = null ; private static MethodInfo orderbyDecInfo = null ; public static IQueryable<T> OrderBy<T>( this IQueryable<T> query, string property) where T : class { Type entityType = typeof (T); Type entityPropertyType = entityType.GetProperty(property).PropertyType; var orderPara = Expression.Parameter(entityType, "o" ); var orderExpr = Expression.Lambda(Expression.Property(orderPara, property), orderPara); if (orderbyInfo== null ) { //因為呼叫OrderBy需要知道型別,不知道的情況下無法直接呼叫,所以用反射的方式呼叫 //泛型的GetMethod很難,所以用GetMethods在用Linq取出Method,找到後快取。 orderbyInfo = typeof (Queryable).GetMethods().Single(x => x.Name == "OrderBy" && x.GetParameters().Length == 2); } //因為是泛型Mehtod要呼叫MakeGenericMethod決定泛型型別 return orderbyInfo.MakeGenericMethod( new Type[] { entityType, entityPropertyType }).Invoke( null , new object [] { query, orderExpr }) as IQueryable<T>; } public static IQueryable<T> OrderByDescending<T>( this IQueryable<T> query, string property) { Type entityType = typeof (T); Type entityPropertyType = entityType.GetProperty(property).PropertyType; var orderPara = Expression.Parameter(entityType, "o" ); var orderExpr = Expression.Lambda(Expression.Property(orderPara, property), orderPara); if (orderbyDecInfo == null ) { orderbyDecInfo = typeof (Queryable).GetMethods().Single(x => x.Name == "OrderByDescending" && x.GetParameters().Length == 2); } return orderbyDecInfo.MakeGenericMethod( new Type[] { entityType, entityPropertyType }).Invoke( null , new object [] { query, orderExpr }) as IQueryable<T>; } } |
使用Expression做Linq的參數化排序的更多相关文章
- 02.C#可空類型、默認參數、LINQ(一章1.3-1.4)
利用上班時間發個隨筆,不知領導會不會看到,可能會有同事看到也說不定啊:) 關于可空類型,在C#1中沒有這個概念,在C#3中引入的.那比如我們要實現一個表示人的類,人有名字和年齡兩個屬性,如何表示一個沒 ...
- TestNG的參数化測试、共享线程池配置、參数默认值配置
在使用TestNG进行測试时,常常会使用到一些參数化配置,比方数据库.连接池.线程池数. 使用TestNG的參数@Parameter注解进行自己主动化读取 原创文章,版权全部.同意转载,标明出处:ht ...
- 使用 new Q_max_capacity 參數,同樣 loading 下,粗估耗電量(UI 上的 %)。
Precondition : 除了 Q_max 外,其它參數皆同. old Q_max_capacity : 1500 mAh new Q_max_capacity : 2200 mAh 有一個 lo ...
- QTP自带订票实现循环执行,參数化和将异常提示信息输出
做这个样例主要是为了积累一些较基础的知识,以便日后可參考学习 这个样例是一个订票的C/Sclient程序 一.业务需求: 1.实现异常和正常数据登录窗体,系统对数据进行校验 2.登录成功后.进行订票业 ...
- Linux下安裝Oracle database內核參數設置
參考:1529864.1 ************************************************** RAM ...
- 同一個Loader對象傳入不同參數時,从数据库中查询的結果每次都一樣
發現問題: LoaderManager().initLoader()方法調用時會根據第一個參數ID去判斷是否已經存在一個Loader加載器,如果存在則複 用,不存在則建一個新的加載器.由於我第一次已經 ...
- shell傳遞參數
Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 比如我們 ...
- 設定 gpio 為 讀取用途,需注意的參數
Schematic 解說 上面的 線路圖, R1 R2 只能有一個被接上, R3 R4 只能有一個被接上, 是使用 gpio 讀取 電壓 判斷為0 或是 1 這時的 gpio 設定,其中一個參數需設為 ...
- 在 kernel 下打出 有帶參數的log。 怪異現象與解決方式。
code battery_log(BAT_LOG_CRTI, "youchihwang abc10010 xxxaaa8-2\r\n"); battery_log(BAT_LOG_ ...
随机推荐
- 《ASP.NET1200例》C# WINFORM程序的三层架构如何建立的。
先添加-新建项目-windows应用程序,然后在右边的解决方案资源管理器上面,在当前的解决方案上面右击,点,添加-新建项目-类库,分别建立.DAL,BLL,Model三个项目,然后,在DAL项目上右击 ...
- 【JAVA、C++】LeetCode 001 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 基于centos搭建nginx+uwsgi运行django环境
环境: CentOS 7 nginx/1.9.12 Python 2.7.5 一:安装依赖包5 yum install zlib-devel bzip2-devel pcre-devel openss ...
- hdu 1113 Word Amalgamation 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 题意:输入一个字典,然后再输入若干单词(每行中,1 <= 单词数 <= 100,并且 ...
- 10件在PHP 7中不要做的事情
刚刚在园子里看到一篇特别好的文章,就拿到我的园子里分享了. 1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是 ...
- [Android Pro] 监听WIFI 打开广播
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-perm ...
- php 面向对象之继承、多态和静态方法
<?php //继承:子类可以继承父类的一切 //特点:单继承 //父类 class Ren { public $name; public $sex; public $yuyan; functi ...
- 安装phpredis模块
[root@web01 src]# wget github.com/owlient/phpredis/tarball/master [root@web01 src]# tar -xzvf master ...
- iOS开发网络篇—JSON介绍
一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典和数组 {" ...
- php怎么判断网页是电脑访问还是手机访问
.第一种方法 <?php function check_wap() { ) { ;$i<count($list);$i++){ ){ ...