偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

實例講解Linq動態(tài)條件查詢

開發(fā) 后端
Linq動態(tài)條件目前已經(jīng)應(yīng)用的比較廣泛,但是熟練掌握其方法的人還不是很多,所以大多數(shù)開發(fā)者都有或多或少的疑問。今天筆者就來用實例為大家解決這些疑問。

Linq動態(tài)條件目前已經(jīng)應(yīng)用的比較廣泛,但是熟練掌握其方法的人還不是很多,所以大多數(shù)開發(fā)者都有或多或少的疑問。今天筆者就來用實例為大家解決這些疑問。

在開發(fā)項目的過程中,我們經(jīng)常會遇到這樣的需求,動態(tài)組合條件的查詢。比如淘寶中的高級搜索:

LinQ動態(tài)組合條件的查詢
實例講解Linq動態(tài)條件查詢

要實現(xiàn)這個功能,通常的做法是拼接SQL查詢字符串,不管是放在程序中或是在存儲過程中?,F(xiàn)在出現(xiàn)了Linq,下面來看看Linq動態(tài)條件查詢是怎樣實現(xiàn)的。

還是以Northwind數(shù)據(jù)庫為例,如果要查詢所有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"));  

不過如果該需求不確定呢?我們不知道具體是哪些字母,可能傳過來的是一個字符串?dāng)?shù)組:

  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動態(tài)條件中提供一些方法允許我們動態(tài)構(gòu)造Lambda表達(dá)式。如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 });  

現(xiàn)在來解釋Linq動態(tài)條件這段代碼,首先構(gòu)造了一個ParameterExpression對象,它作為參數(shù)傳到Lambda表達(dá)中(相當(dāng)于c => c.CustomerID.StartsWith("A")這里的c)。然后用值為false的Expression用來初始化該表達(dá)式(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方法動態(tài)構(gòu)造一個表達(dá)式,其中Expression.Property(c, typeof(Customer).GetProperty("CustomerID"))轉(zhuǎn)換為c.CustomerID,typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) })表示string的StartsWith(string)方法,Expression.Constant(s)表示字符串常量,這個方法可以表示成:c.CustomerID.StartsWith(s)。然后調(diào)用Expression.Or方法組合各個條件(根據(jù)邏輯關(guān)系不同調(diào)用不同的方法,如or,and...)。

最后構(gòu)造成Lambda表達(dá)式,現(xiàn)在就可以使用它了 ctx.Customers.Where(end)。

對于Linq動態(tài)條件查詢中,這個并不是最好的解決方法,使用這種方式生成的Lambda表達(dá)式,由于用到了反射,這樣編譯器不能檢查錯誤,很可能因為一個字母寫錯導(dǎo)致運行時拋出異常。因此,要用一個更好的方案,既能滿足我們的要求,又能讓編譯器更好的支持,這個方法筆者還未實現(xiàn),就請你和筆者一起努力吧。

【編輯推薦】

  1. LINQ動態(tài)查詢的實現(xiàn)淺析
  2. LINQ TO SQL動態(tài)修改表名稱的實現(xiàn)淺析
  3. LINQ To SQL的一點討論
  4. 淺析LINQ事務(wù)處理的實現(xiàn)
  5. 淺析DataSet和DataTable
責(zé)任編輯:阡陌 來源: 博客園
相關(guān)推薦

2009-09-15 10:16:01

LINQ動態(tài)查詢

2009-09-15 09:19:22

linq動態(tài)條件

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-17 16:20:43

Linq to sql

2009-09-14 19:14:51

LINQ動態(tài)查詢

2009-09-17 17:03:13

LINQ動態(tài)查詢

2010-11-22 16:22:39

MySQL連接查詢

2011-07-06 16:15:46

iPhone 圖片

2009-09-07 20:40:48

LINQ子查詢

2009-07-15 13:11:25

ibatis動態(tài)查詢

2010-05-18 09:02:55

MySQL條件查詢

2009-09-17 14:21:19

LINQ表達(dá)式

2009-03-23 10:47:43

數(shù)據(jù)庫SQLLINQ

2009-09-18 16:32:51

Linq委托實例化

2009-09-17 13:10:48

linq動態(tài)排序

2009-09-15 10:46:04

LINQ to SQL
點贊
收藏

51CTO技術(shù)棧公眾號