中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

將不確定變為(wei)確定~表達式(shi)樹是否可以有個集合,條(tiao)件過濾有了新方向(xiang)

回到目錄

對(dui)于我(wo)之(zhi)前項目(mu)中的(de)統一條件(jian)過濾采用了dictinary來實現的(de),優點(dian)就是(shi)方法簽(qian)名統一了,缺(que)點(dian)不用說,就是(shi)字典的(de)鍵(jian)容易寫錯,感(gan)覺一進入.net3.5之(zhi)后,一切都要和Expression聯系(xi)在一起,我(wo)們在創建一個Expression(表達式(shi)(shi)樹(shu))時,可以使用lambda表達式(shi)(shi)去創建,很(hen)容易:

1 Expression<Func<string, bool>> predicate= name=>name=="zzl";

可以看到,它其(qi)它由一個(ge)(ge)(ge)委托組成,輸入參數是個(ge)(ge)(ge)字(zi)符,輸出是個(ge)(ge)(ge)布爾值(zhi),在(zai)LINQ中(zhong)這種技術(shu)被廣泛的使用在(zai)擴展方(fang)(fang)法中(zhong),如Where擴展方(fang)(fang)法:

 1  public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
 2         {
 3             if (source == null)
 4             {
 5                 throw System.Linq.Error.ArgumentNull("source");
 6             }
 7             if (predicate == null)
 8             {
 9                 throw System.Linq.Error.ArgumentNull("predicate");
10             }
11             return source.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof(TSource) }), new Expression[] { source.Expression, Expression.Quote(predicate) }));
12         }

無(wu)可(ke)厚非,表達式(shi)樹的出現,lambda表達式(shi)的支持,是.net開發人員的福音(yin),也是.net3.5中更大的亮點(dian)了(le)。

說正文了,以(yi)前我的寫統一的參數簽名時,使(shi)用的是dictionary,代碼(ma)中可以(yi)是這(zhe)樣(yang)的

1         Common.VPredication vp = new Common.VPredication();
2             Common.PagingParam pp = new Common.PagingParam(page ?? 1, VConfig.WebConstConfig.PageSize);
3 
4             vp.AddItem("userId", userID);
5             if (_gid != 0)
6                 vp.AddItem("gid", _gid);

統一的方法簽名:

1  Common.Page.PagedList<Entity.Res_Item> GetList(Common.VPredication vp, Common.PagingParam pp)

而有了表(biao)達式樹的(de)集合后,完成(cheng)可(ke)以(yi)把過濾條件寫(xie)在它里面,這(zhe)(zhe)樣構建條件變成(cheng)了這(zhe)(zhe)樣:

1        PredicateList<UserBases> zzl = new PredicateList<UserBases>();
2             zzl.Add(i => i.Name.Contains("zzl"));
3             zzl.Add(i => i.UserID == 1);
4             GetModel(zzl).ForEach(i => Console.WriteLine(i.UserID + i.Name));

而方法(fa)簽名仍(reng)然是很統一,只是變成了表達式樹的(de)樣(yang)子,有點強類型的(de)味道,呵呵:

1 List<UserBases> GetModel(PredicateList<UserBases> param)

而PredicateList類型的原代碼,我也公(gong)開一下吧(ba),呵(he)呵(he),大家(jia)一共分(fen)享:

 1    /// <summary>
 2     /// 功(gong)能:條(tiao)件過濾類
 3     /// 作者:張占(zhan)嶺
 4     /// 日期(qi):2012-6-7
 5     /// </summary>
 6     public class PredicateList<TEntity> : IEnumerable<Expression<Func<TEntity, bool>>> where TEntity : class
 7     {
 8         List<Expression<Func<TEntity, bool>>> expressionList;
 9         public PredicateList()
10         {
11             expressionList = new List<Expression<Func<TEntity, bool>>>();
12         }
13         /// <summary>
14         /// 添加到集合(he)
15         /// </summary>
16         /// <param name="predicate"></param>
17         public void Add(Expression<Func<TEntity, bool>> predicate)
18         {
19             expressionList.Add(predicate);
20         }
21         /// <summary>
22         /// 從集合中移除
23         /// </summary>
24         /// <param name="predicate"></param>
25         public void Remove(Expression<Func<TEntity, bool>> predicate)
26         {
27             expressionList.Remove(predicate);
28         }
29         #region IEnumerable 成員
30 
31         public IEnumerator GetEnumerator()
32         {
33             return expressionList.GetEnumerator();
34         }
35 
36         #endregion
37 
38         #region IEnumerable<Expression<Func<TEntity>>> 成員
39 
40         IEnumerator<Expression<Func<TEntity, bool>>> IEnumerable<Expression<Func<TEntity, bool>>>.GetEnumerator()
41         {
42             return expressionList.GetEnumerator();
43         }
44 
45         #endregion
46     }

 回到目錄

posted @ 2012-06-07 13:49  張占嶺  閱讀(2049)  評論(8)    收藏  舉報