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的更多相关文章

  1. linq字符串搜索条件,排序条件-linq动态查询语句 Dynamic LINQ

    在做搜索和排序的时候,往往是前台传过来的字符串做条件,参数的数量还不定,这就需要用拼sql语句一样拼linq语句.而linq语句又是强类型的,不能用字符串拼出来. 现在好了,有个开源的linq扩展方法 ...

  2. Extension Method[下篇]

    四.Extension Method的本质 通过上面一节的介绍,我们知道了在C#中如何去定义一个Extension Method:它是定义在一个Static class中的.第一个Parameter标 ...

  3. 自动化测试尝试 动态Linq表达式生成 ftp上传

    自动化测试尝试   1. Selenium IDE Selenium IDE is a Chrome and Firefox plugin which records and plays back u ...

  4. 通过LINQ表达式树动态构建查询条件

    第一种方法: public static class PredicateExtensions { public static Expression<Func<T, bool>> ...

  5. C# - LINQ 表达式树

    表达式树(Expression Tree) 表达式树是不可执行的代码,它只是用于表示一种树状的数据结构,树上的每一个节点都表示为某种表达式类型,大概有25种表达式类型,它们都派生自Expression ...

  6. Linq lambda 匿名方法

    课程6 委托.匿名方法.Lambda表达式.Linq查询表达式 上课日志1 一.委托的基本认识 提问:能不能把方法作为参数传递??? 也即是能不能声明一个能存放方法的变量呢——委托. 委托是一种数据类 ...

  7. Linq之扩展方法

    目录 写在前面 系列文章 扩展方法 总结 写在前面 上篇文章介绍了隐式类型,自动属性,初始化器,匿名类的相关概念,及通过反编译的方式查看了编译器帮我们做了那些事.本篇文章将介绍扩展方法的知识点,及如何 ...

  8. LINQ之路 5:LINQ查询表达式

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询表达式(Query Expression). LINQ方法语法的本质是通过扩展方法和Lambda表达式来创建查询.C# ...

  9. LINQ之路 4:LINQ方法语法

    书写LINQ查询时又两种语法可供选择:方法语法(Fluent Syntax)和查询语法(Query Expression). LINQ方法语法是非常灵活和重要的,我们在这里将描述使用链接查询运算符的方 ...

随机推荐

  1. S_ISREG等几个常见的宏 struct stat

    S_ISLNK(st_mode):是否是一个连接.S_ISREG(st_mode):是否是一个常规文件.S_ISDIR(st_mode):是否是一个目录S_ISCHR(st_mode):是否是一个字符 ...

  2. 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

    GCC编译C源程序时出现:错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token,通常是因为在函数声明(包括包含的头文 ...

  3. Jordan Lecture Note-9: Principal Components Analysis (PCA).

    Principal Components Analysis (一)引入PCA    当我们对某个系统或指标进行研究时往往会发现,影响这些系统和指标的因素或变量的数量非常的多.多变量无疑会为科学研究带来 ...

  4. java 开源缓存框架--转载

    原文地址:http://www.open-open.com/13.htm  JBossCache/TreeCache  JBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的 ...

  5. grep时排除指定的文件和目录

    参考:http://winterth.duapp.com/notes/ar03s04.htmlhttp://blog.sina.com.cn/s/blog_7169c8ce0100qkyf.html ...

  6. 小白日记5:kali渗透测试之被动信息收集(四)--theHarvester,metagoofil,meltag,个人专属密码字典--CUPP

    1.theHarvester theHarvester是一个社会工程学工具,它通过搜索引擎.PGP服务器以及SHODAN数据库收集用户的email,子域名,主机,雇员名,开放端口和banner信息. ...

  7. Ubuntu 下安装VNC server

    尽管我们在大部分情况下用ssh登录Ubuntu服务器就好了,但是有时候我们的程序需要在图形界面下运行,这时我们就要用到vnc server这个软件了.在Ubuntu下安装vnc server很简单的, ...

  8. shell判断一个变量是否为空

    判断一个变量是否为空 . 1. 变量通过" "引号引起来 如下所示:,可以得到结果为 IS NULL. #!/bin/sh para1= if [ ! -n "$para ...

  9. shell(1)

    bash变量类别: 本地变量 : 又叫局部变量,仅对当前shell进程有效 环境变量 : 当前shell及其子shell,子子shell-. 特殊变量 : $?  上一个命令执行的状态,0表示执行成功 ...

  10. HDU 1058 Humble Numbers (DP)

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...