动态linq表达式新方法,Dynamic LINQ Extension Method
Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its implementation available as a Nuget package, but, like I said, you already have it in your machine, hidden inside the System.Web.Extensions assembly.
In order to make it easier to use, I wrote a simple extension method that works with plain old IQueryable<T>. And here it is:
1: public static IQueryable<T> Where<T>(this IQueryable<T> query, String restriction, params Object[] values)
2: {
3: Assembly asm = typeof(UpdatePanel).Assembly;
4: Type dynamicExpressionType = asm.GetType("System.Web.Query.Dynamic.DynamicExpression");
5: MethodInfo parseLambdaMethod = dynamicExpressionType.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => (m.Name == "ParseLambda") && (m.GetParameters().Length == 2)).Single().MakeGenericMethod(typeof(T), typeof(Boolean));
6: Expression<Func<T, Boolean>> expression = parseLambdaMethod.Invoke(null, new Object[] { restriction, values }) as Expression<Func<T, Boolean>>;
7:
8: return (query.Where(expression));
9: }
It even supports parameters! Just two simple examples – I am using Entity Framework, but you can use whatever you like, this is totally generic:
1: //without parameters
2: var productsWithPriceGreaterThan100 = ctx.Products.Where("Price > 100").ToList();
3:
4: //with parameters
5: var productsWithPriceLowerThan100 = ctx.Products.Where("Price < @0", 100).ToList();
To make it clear, all parameters must be indicated as @0, @1, and so on. It is better to use them, because the database engine can reuse the execution plan.
There’s one limitation, though: you can’t compare each value on its own, you have to specify one of its properties. For example, you can’t have:
1: var productNames = ctx.Products.Select(x => x.Name).Where("@it != @0", "Some Name").ToList();
The @it parameter is not recognized, which is a pity.
Make sure you have references to both System.Web and System.Web.Extensions, this is required, and won’t affect anything.
As always, glad to be of service! ![]()
动态linq表达式新方法,Dynamic LINQ Extension Method的更多相关文章
- linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ
在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...
- Extension Method[下篇]
四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...
- 自动化测试尝试 动态Linq表达式生成 ftp上传
自动化测试尝试 1. Selenium IDE Selenium IDE is a Chrome and Firefox plugin which records and plays back u ...
- 通过LINQ表达式树动态构建查询条件
第一种方法: public static class PredicateExtensions { public static Expression<Func<T, bool>> ...
- C# - LINQ 表达式树
表达式树(Expression Tree) 表达式树是不可执行的代码,它只是用于表示一种树状的数据结构,树上的每一个节点都表示为某种表达式类型,大概有25种表达式类型,它们都派生自Expression ...
- Linq lambda 匿名方法
课程6 委托.匿名方法.Lambda表达式.Linq查询表达式 上课日志1 一.委托的基本认识 提问:能不能把方法作为参数传递??? 也即是能不能声明一个能存放方法的变量呢——委托. 委托是一种数据类 ...
- Linq之扩展方法
目录 写在前面 系列文章 扩展方法 总结 写在前面 上篇文章介绍了隐式类型,自动属性,初始化器,匿名类的相关概念,及通过反编译的方式查看了编译器帮我们做了那些事.本篇文章将介绍扩展方法的知识点,及如何 ...
- LINQ之路 5:LINQ查询表达式
书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询表达式(Query Expression). LINQ方法语法的本质是通过扩展方法和Lambda表达式来创建查询.C# ...
- LINQ之路 4:LINQ方法语法
书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询语法(Query Expression). LINQ方法语法是非常灵活和重要的,我们在这里将描述使用链接查询运算符的方 ...
随机推荐
- 让java程序在后台一直执行(例如putty关闭后后台程序继续运行)
如果在终端中执行java -jar xxx.jar&命令,当终端关闭时,xxx.jar也会同时结束运行,但是如果执行nohup java -jar xxx.jar&命令,则程序会在后台 ...
- ElasticSearch使用
安装之前,请参考https://github.com/richardwilly98/elasticsearch-river-mongodb根据你的MongoDB版本号决定需要的elasticsearc ...
- [AngularJS] Introduction to ui-router
Introduce to basic $stateProvider.state() with $stateParams services. Understand how nested router w ...
- [AngularJS] Lazy loading Angular modules with ocLazyLoad
With the ocLazyLoad you can load AngularJS modules on demand. This is very handy for runtime loading ...
- IDispatch接口 - GetIDsOfNames和Invoke(转)
IDispatch接口是COM自动化的核心.其实,IDispatch这个接口本身也很简单,只有4个方法: IDispatch : public IUnknown { public: virtual H ...
- 用CAS操作实现Go标准库中的Once
Go标准库中提供了Sync.Once来实现"只执行一次"的功能.学习了一下源代码,里面用的是经典的双重检查的模式: // Once is an object that will p ...
- STM32W108芯片的SWD在IAR7.30版本中不能用
提示说0x20000B8不能读,When Clear soft RAM BP
- Swift 玩转gif
众所周知,iOS默认是不支持gif类型图片的显示的,但是我们项目中常常是需要显示gif为动态图片.那肿么办?第三方库?是的 ,很多第三方都支持gif , 如果一直只停留在用第三方上,技术难有提高.上版 ...
- Django单元测试二三事
零.前言 之前做过一个微信公众平台的开发者后台,功能比较简单,我个人也比较懒,所以就没有写测试.前段时间更新了一下版本,对代码进行了改动.结果昨天收到消息说后台出问题了,一个功能无法使用.我检查了半天 ...
- linux云计算集群架构学习笔记:命令查看文件内容
查看文件内容 1.cat 命令 作用:查看文件内容 语法:cat 文件名 2. more 命令 作用:分页查看文件内容 语法:more 文件名 例:more /etc/passwd 按下回车刷新一行 ...