动态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方法语法是非常灵活和重要的,我们在这里将描述使用链接查询运算符的方 ...
随机推荐
- Codeforces 278C Learning Languages(并查集)
题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通. 注意所有人都不会任何语言的时候,答案是n而不是n-1. #include<algorithm> #include<iost ...
- [Bower] Bower
//search bower search jquery bower search jquery | grep formstyler //info bower info jquery //instal ...
- 解决Server Error in '/' Application.方法!
<!-- Web.Config Configuration File --> <configuration> <system.web> ...
- 模板方法模式(Template Method)
@@@模式定义: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. 模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. @@@练习示例: 登录控制 @@@示例代码: \s ...
- Linux 学习笔记 cp 和 ln
最近在重温大学的经典课程:Linux.每天工作之余的时间,坐在家里的阳台上,简简单单的纪录一些基本的知识点,对我来说,既温暖又难得. 有朋友问我关于cp和ln建立符号链接(软链接)和硬链接的一些问题, ...
- Entity Framework实体框架使用TrackerEnabledDbContext进行操作日志跟踪
在EF实体框架中进行日志跟踪,一般都是自己写个Log实体类,在数据保存时进行属性原始值验证来进行日志跟踪.当然还可以使用一些第三扩展库例如:entity framework extended进行日志记 ...
- Autowired properities class
1. Properties类 @ConfigurationProperties(locations = "classpath:build.properties") @JsonInc ...
- POWER DESIGN
一.概念数据模型概述数据模型是现实世界中数据特征的抽象.数据模型应该满足三个方面的要求:1)能够比较真实地模拟现实世界2)容易为人所理解3)便于计算机实现 概念数据模型也称信息模型,它以实体-联系(E ...
- python(5)- sys.stdout()实现进度条
1. 使用\r , 让其始终在行首输出,实现进度条 import sys, time ''' 使用\r 来实现进度条的效果,\r 是光标移到行首但不换行. 假设文件大小为60,一下下载1, 下载到60 ...
- plsql常用快捷键
plsql使用技巧 1.类SQL PLUS窗口:File->New->Command Window,这个类似于oracle的客户端工具sql plus,但比它好用多了. 2.设置关键字自动 ...