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

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

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
如何解決用戶Linq自定義組合查詢

如何解決Linq自定義組合查詢?看起來似乎是個不太難的問題。但很多人仍然會卡在這里不知如何下手,今天我就用我的經歷來給大家做一個例子。

創(chuàng)新互聯(lián)建站 - 成都服務器托管,四川服務器租用,成都服務器租用,四川網通托管,綿陽服務器托管,德陽服務器托管,遂寧服務器托管,綿陽服務器托管,四川云主機,成都云主機,西南云主機,成都服務器托管,西南服務器托管,四川/成都大帶寬,機柜大帶寬租用·托管,四川老牌IDC服務商

當年,俺被誤導,說是怎么實現(xiàn)Linq自定義組合捏?因為Linq是預編譯滴,沒有辦法想拼一個sql字符串出來,讓我糾結很久,但是,我覺得微軟的人應該比較厲害,所以我本著“有困難要上,沒有困難制造困難也要上”的原則,在還沒有熟悉LINQ TO ADO.NET的情況下,我覺得決定在最近的我自己獨立完成小項目里使用ASP.NET MVC + ADO.NET EF。

一般的信息系統(tǒng)都有一個組合查詢的功能,我很快用jquery做了這樣一個功能。

這個表單將Linq自定義組合條件提交后臺,我先將它封裝成條件對象的數(shù)組。

 
 
 
 
  1. ///   
  2. /// 條件  
  3. /// 
  4.  
  5. public class Condition  
  6. {  
  7.     ///   
  8.     /// 字段  
  9.     /// 
  10.  
  11.     public string Field { getset; }  
  12.     ///   
  13.     /// 表達式  
  14.     /// 
  15.  
  16.     public string Operator { getset; }  
  17.     ///   
  18.     /// 值  
  19.     /// 
  20.  
  21.     public string Value { getset; }  
  22.     ///   
  23.     /// 關系  
  24.     /// 
  25.  
  26.     public string Relation { getset; }  
  27.  
  28.     ///   
  29.     ///   
  30.     /// 
  31.  
  32.     ///   
  33.     ///   
  34.     ///   
  35.     ///   
  36.     ///   
  37.     public static Condition[] BuildConditions(string[] fileds,string[] operators,string[] values,string[] relations)  
  38.     {  
  39.         if (fileds == null || operators == null || values == null || relations == null)  
  40.         {  
  41.             return null;  
  42.         }  
  43.         Condition[] conditions = new Condition[fileds.Length];  
  44.         try 
  45.         {  
  46.             for (int i = 0; i < conditions.Length; i++)  
  47.             {  
  48.                 conditions[i] = new Condition()  
  49.                 {  
  50.                     Field = fileds[i],  
  51.                     Operator = operators[i],  
  52.                     Value = values[i],  
  53.                     Relation = relations[i]  
  54.                 };  
  55.             }  
  56.         }  
  57.         catch 
  58.         {  
  59.             return null;  
  60.         }  
  61.         return conditions;  
  62.     }  

#p#

實際上,編譯器是把Linq自定義表達式編譯成expression tree的形式,我只需要將條件對象數(shù)組轉換為expression tree就可以了。

我先將一個條件轉化為一個簡單的expression。

 
 
 
 
  1. ///   
  2. ///   
  3. /// 
  4.  
  5. ///   
  6. ///   
  7. ///   
  8. private static Expression ConditonToExpression(Condition condition,Expression parameter)  
  9. {  
  10.     Expression expr = null;  
  11.     Type type = typeof(EDM_Resource);  
  12.  
  13.     PropertyInfo pi = type.GetProperty(condition.Field);  
  14.     Expression left = Expression.Property(parameter, pi);  
  15.  
  16.     object value = Convert.ChangeType(condition.Value, pi.PropertyType);  
  17.     Expression right = Expression.Constant(value);  
  18.     switch (condition.Operator)  
  19.     {  
  20.         case "=":  
  21.             expr = Expression.Equal(left, right);  
  22.             break;  
  23.         case "<":  
  24.             expr = Expression.LessThan(left, right);  
  25.             break;  
  26.         case "<=":  
  27.             expr = Expression.LessThanOrEqual(left, right);  
  28.             break;  
  29.         case ">":  
  30.             expr = Expression.GreaterThan(left, right);  
  31.             break;  
  32.         case ">=":  
  33.             expr = Expression.GreaterThanOrEqual(left, right);  
  34.             break;  
  35.     }  
  36.     return expr;  

#p#

然后組合,變成一個lamda表達式,追加到where上。

 
 
 
 
  1. ///   
  2. ///   
  3. /// 
  4.  
  5. ///   
  6. ///   
  7. ///   
  8. ///   
  9. ///   
  10. ///   
  11. public IList  FindByGroup(EDM_ResGroup resGroup, Condition[] conditions,  int first, int limit, out int count)  
  12. {  
  13.     using (ShengjingEDM2Entities context = new ShengjingEDM2Entities())  
  14.     {  
  15.         IQueryable  result = DoFindByGroup(resGroup, context);  
  16.         ParameterExpression parameter = Expression.Parameter(typeof(EDM_Resource), "r");  
  17.         Expression body = null;  
  18.  
  19.         if (conditions != null && conditions.Length > 0)  
  20.         {  
  21.             body = ConditonToExpression(conditions[0], parameter);  
  22.             for (int i = 1; i < conditions.Length; i++)  
  23.             {  
  24.                 Expression right = ConditonToExpression(conditions[i],parameter);  
  25.                 body = conditions[i - 1].Relation.ToUpper().Equals("AND") ?  
  26.                     Expression.And(body, right) :  
  27.                     Expression.Or(body, right);  
  28.             }  
  29.         }  
  30.  
  31.         if (body != null)  
  32.         {  
  33.             Expression bool>> expr = Expression.Lambda bool>>(body, parameter);  
  34.             result = result.Where (expr);  
  35.         }  
  36.         result = result.OrderByDescending int>(r => r.ResourceID);  
  37.         count = result.Count ();  
  38.         return result  
  39.             .Skip (first)  
  40.             .Take (limit)  
  41.             .ToList ();  
  42.     }  

原來Linq自定義這么強大,這么爽,比拼where條件的方法優(yōu)雅多了,開發(fā)效率也是提高不少,而且我發(fā)現(xiàn)性能也不錯,100萬級的數(shù)據通過索引和分頁查詢還算可以。


分享名稱:如何解決用戶Linq自定義組合查詢
本文路徑:http://www.5511xx.com/article/cdicidi.html