日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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

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

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

實(shí)例講解Linq動(dòng)態(tài)條件查詢

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

還是以Northwind數(shù)據(jù)庫為例,如果要查詢所有CustomerID以A或者B字母開頭的Customer,一般我們會(huì)這樣寫:

 
 
 
 
  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"));  

不過如果該需求不確定呢?我們不知道具體是哪些字母,可能傳過來的是一個(gè)字符串?dāng)?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動(dòng)態(tài)條件中提供一些方法允許我們動(dòng)態(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.  Expression bool>> end =  
  13.      Expression.Lambda bool>>
  14. (condition, new ParameterExpression[] { c });  

現(xiàn)在來解釋Linq動(dòng)態(tài)條件這段代碼,首先構(gòu)造了一個(gè)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方法動(dòng)態(tài)構(gòu)造一個(gè)表達(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)表示字符串常量,這個(gè)方法可以表示成:c.CustomerID.StartsWith(s)。然后調(diào)用Expression.Or方法組合各個(gè)條件(根據(jù)邏輯關(guān)系不同調(diào)用不同的方法,如or,and...)。

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

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


文章標(biāo)題:實(shí)例講解Linq動(dòng)態(tài)條件查詢
路徑分享:http://www.5511xx.com/article/cojphcd.html