日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
實例講解Linq動態(tài)條件查詢

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

曲周網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司

在開發(fā)項目的過程中,我們經(jīng)常會遇到這樣的需求,動態(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"));  

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

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

我們可能很自然的這樣寫,雖然很清楚要做什么,但是很可惜編譯不通過,編譯器并不允許我們像這樣來組合條件。

 
 
 
 
  1. Expression bool>> 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表達式。如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.  Expression bool>> end =  
  13.      Expression.Lambda bool>>
  14. (condition, new ParameterExpression[] { c });  

現(xiàn)在來解釋Linq動態(tài)條件這段代碼,首先構(gòu)造了一個ParameterExpression對象,它作為參數(shù)傳到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方法動態(tài)構(gòu)造一個表達式,其中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表達式,現(xiàn)在就可以使用它了 ctx.Customers.Where(end)。

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


網(wǎng)站題目:實例講解Linq動態(tài)條件查詢
當前鏈接:http://www.dlmjj.cn/article/cojphcd.html