实例讲解Linq动态条件查询

开发 后端
Linq动态条件目前已经应用的比较广泛,但是熟练掌握其方法的人还不是很多,所以大多数开发者都有或多或少的疑问。今天笔者就来用实例为大家解决这些疑问。

Linq动态条件目前已经应用的比较广泛,但是熟练掌握其方法的人还不是很多,所以大多数开发者都有或多或少的疑问。今天笔者就来用实例为大家解决这些疑问。

在开发项目的过程中,我们经常会遇到这样的需求,动态组合条件的查询。比如淘宝中的高级搜索:

LinQ动态组合条件的查询
实例讲解Linq动态条件查询

要实现这个功能,通常的做法是拼接SQL查询字符串,不管是放在程序中或是在存储过程中。现在出现了Linq,下面来看看Linq动态条件查询是怎样实现的。

还是以Northwind数据库为例,如果要查询所有CustomerID以A或者B字母开头的Customer,一般我们会这样写:

  1. var results = ctx.Customers.Where(c => c.CustomerID.StartsWith("A") || 
  2. c.CustomerID.StartsWith("B")); 

如果需求改变,还要查询出以X字母或者Y字母开头的Customer,那可以增加查询条件:

  1. var results = ctx.Customers.Where(c => c.CustomerID.StartsWith("A") || 
  2. c.CustomerID.StartsWith("B")  
  3.     || c.CustomerID.StartsWith("X") || c.CustomerID.StartsWith("Y"));  

不过如果该需求不确定呢?我们不知道具体是哪些字母,可能传过来的是一个字符串数组:

  1. string[] starts = ....  
  2. var results = ctx.Customers.Where(c => ?); 

我们可能很自然的这样写,虽然很清楚要做什么,但是很可惜编译不通过,编译器并不允许我们像这样来组合条件。

  1. Expressionbool>> condition = cus => true;  
  2. foreach (string s in starts)  
  3. {  
  4.     condition = cus => cus.CustomerID.StartsWith(s) || condition(cus);  

在Linq动态条件中提供一些方法允许我们动态构造Lambda表达式。如Expression.Call, Expression.Or, Expression.And,这样代码就可以写成:

  1. ParameterExpression c = Expression.Parameter(typeof(Customer), "c");  
  2.  Expression condition = Expression.Constant(false);  
  3.  foreach (string s in starts)  
  4.  {  
  5.      Expression con = Expression.Call(  
  6.          Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),  
  7.          typeof(string).GetMethod("StartsWith"
  8. new Type[] { typeof(string) }),  
  9.          Expression.Constant(s));  
  10.      condition = Expression.Or(con, condition);  
  11.  }  
  12.  Expressionbool>> end =  
  13.      Expression.Lambdabool>>
  14. (condition, new ParameterExpression[] { c });  

现在来解释Linq动态条件这段代码,首先构造了一个ParameterExpression对象,它作为参数传到Lambda表达中(相当于c => c.CustomerID.StartsWith("A")这里的c)。然后用值为false的Expression用来初始化该表达式(Expression.Constant(false))。

  1. Expression con = Expression.Call(  
  2.      Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),  
  3.      typeof(string).GetMethod("StartsWith"new Type[] { typeof(string) }),  
  4.      Expression.Constant(s));  
  5.  condition = Expression.Or(con, condition);  

上面这段代码是重头戏,用Expression.Call方法动态构造一个表达式,其中Expression.Property(c, typeof(Customer).GetProperty("CustomerID"))转换为c.CustomerID,typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) })表示string的StartsWith(string)方法,Expression.Constant(s)表示字符串常量,这个方法可以表示成:c.CustomerID.StartsWith(s)。然后调用Expression.Or方法组合各个条件(根据逻辑关系不同调用不同的方法,如or,and...)。

最后构造成Lambda表达式,现在就可以使用它了 ctx.Customers.Where(end)。

对于Linq动态条件查询中,这个并不是最好的解决方法,使用这种方式生成的Lambda表达式,由于用到了反射,这样编译器不能检查错误,很可能因为一个字母写错导致运行时抛出异常。因此,要用一个更好的方案,既能满足我们的要求,又能让编译器更好的支持,这个方法笔者还未实现,就请你和笔者一起努力吧。

【编辑推荐】

  1. LINQ动态查询的实现浅析
  2. LINQ TO SQL动态修改表名称的实现浅析
  3. LINQ To SQL的一点讨论
  4. 浅析LINQ事务处理的实现
  5. 浅析DataSet和DataTable
责任编辑:阡陌 来源: 博客园
相关推荐

2009-09-15 10:16:01

LINQ动态查询

2009-09-15 09:19:22

linq动态条件

2009-09-15 09:33:46

linq多条件查询

2009-09-17 16:46:34

Linq to sql

2009-09-18 15:15:12

LINQ to SQL

2009-09-15 11:34:47

Linq多条件查询

2009-09-14 17:03:32

LINQ模糊查询

2009-09-14 19:14:51

LINQ动态查询

2009-09-17 17:03:13

LINQ动态查询

2009-09-17 16:20:43

Linq to sql

2011-07-06 16:15:46

iPhone 图片

2010-11-22 16:22:39

MySQL连接查询

2009-09-07 20:40:48

LINQ子查询

2009-07-15 13:11:25

ibatis动态查询

2010-05-18 09:02:55

MySQL条件查询

2009-03-23 10:47:43

数据库SQLLINQ

2009-09-17 14:21:19

LINQ表达式

2009-09-18 16:32:51

Linq委托实例化

2009-09-14 10:13:02

LINQ查询操作

2009-09-09 16:53:53

LINQ查询语法
点赞
收藏

51CTO技术栈公众号