在前面的一文中我展示了資料表的查詢示例,關鍵的一句代碼:table.Query()傳回字典表的所有資料,示範的例子就這麼用了,但是在實際的使用過程中你不可能每次都取把全表資料取回來,業務的處理需要查詢指定條件的資料這是必要的,把表中的資料全部取回來怎麼辦,再說了,如果記錄上百萬、千萬,估計DbServer和網絡也就崩潰了。
在AgileEAS.NET平台的ORM元件中提供了條件查詢的功能,用于實作where和order的功能,我們來看看ITable接口中的查詢定義:
在Query方法中提供了帶Condition類型參數的重載,Condition對象用于構造一個查詢條件(where … order by …),目前不支援having子句。
在應用開發中條件參數的主要工作是構造條件(Condition),條件由基于的條件元素Element組成和排序元素OrderElement組成,即Element是組成Condition的基本機關,Condition與Element、Element與Element、Condition與Condition都可以組合成新的的Condition對象,以下是其他結構圖:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETORM_13576/image_4.png"></a>
Condition提供了這種樹狀的組合方式,由Condition對象的AddElement和AddCondition兩個重載方法完成這種機制:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETORM_13576/%7B1KRG5JN0)K%7BUKH4%5B%5BB%60%25WM_2.jpg"></a>
Condition與Condition、Condition與Element、Element與Element的組成提供了And與Or兩種組合方式,由枚舉ElementCombineType進行定義:
系統提供了EqualTo(等于)、GreaterThan(大于)、SqlCondition(SQL語句條件)等一共19種條件元素,除SqlCondition條件之外其他的都有其固定的含義,SqlCondition條件用于其他條件無法完成或者說用其他條件實作難度較大的情況下使用,條件元素的類型由枚舉ElementType定義:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETORM_13576/AW_@CKBV(PZTI%7D%5DR%7DGBN%24UP_2.jpg"></a>
各條件的詳細功能請參考AgileEAS.NET平台基礎類庫手冊。
關于條件查詢以及條件的構造的知識差不多也就是這些,講起來也沒有什麼講頭,重點才于如何組合這些條件實作複雜的業務,在今天的案例中我選擇性的示範幾個條件的用法,所使用的資料還是商品字典,達到以下的功能:
1.查詢機關為“1*瓶/瓶”的所有商品,并且按商品編碼排序,示範EqualTo條件與排序條件。
2.查詢商品編碼前1位為“1”的所有商品,并且按商品編碼倒序排序。示範SQL條件的使用。
3.查詢商品編碼前2位為“1a”的所有商品,不處理排序。示範Like條件的使用。
4.查詢記錄ID從100到500的記錄,不處理排序。示範BetWeen條件的使用。
5.查詢記錄ID從100到500并且編碼前1位為“1”的所有商品記錄,不處理排序。示範兩個條件元素And組合的使用。
6.查詢記錄ID從100到5000并且編碼前1位為“1”的所有商品記錄以及商品編碼前2位為“1a”并且機關為“1*瓶/瓶”的商品記錄,不處理排序。示範兩個條件Or組合的使用。
為了實作上述要求,并且保持非DAL層的代碼不涉及具體的資料庫結果,我們需要在生成的DAL代碼中增加上述查詢的實作:
1 /// <summary>
2 /// 根據機關取得産品字典記錄。
3 /// </summary>
4 /// <param name="unit">機關。</param>
5 public void GetProductListByUnit(string unit)
6 {
7 Condition condition = this.CreateCondition();
8 condition.AddElement("UNIT", unit);
9 //condition.AddElement("UNIT", unit, ElementType.EqualTo); //等同
10 //condition.AddElement("UNIT", unit, ElementType.EqualTo, ElementCombineType.And); //等同
11 condition.AddOrderElement("IDN");
12 this.Query(condition);
13 }
14
15 /// <summary>
16 /// 查詢編碼前一位為X的商品記錄。
17 /// </summary>
18 /// <param name="code">機關。</param>
19 public void GetProductListByCode1(string code)
20 {
21 Condition condition = this.CreateCondition();
22 condition.AddElement("SQL", "left(CODE,1)='" + code + "'", ElementType.SqlCondition);
23 //condition.AddElement("SQL", "left(CODE)='" + code + "'", ElementType.SqlCondition, ElementCombineType.And); //等同
24 condition.AddOrderElement("IDN", true);
25 this.Query(condition);
26 }
27
28 /// <summary>
29 /// 查詢編碼前2位為XX的商品記錄。
30 /// </summary>
31 /// <param name="code">機關。</param>
32 public void GetProductListByCode2(string code)
33 {
34 Condition condition = this.CreateCondition();
35 condition.AddElement("CODE", code, ElementType.MatchPrefix);
36 //condition.AddElement("CODE", code, ElementType.MatchPrefix, ElementCombineType.And); //等同
37 this.Query(condition);
38 }
39
40 /// <summary>
41 /// 查詢IDN從X到Y的記錄。
42 /// </summary>
43 /// <param name="iStart">X</param>
44 /// <param name="iEnd">Y</param>
45 public void GetProductListByIDN(int iStart, int iEnd)
46 {
47 Condition condition = this.CreateCondition();
48
49 List<int> values = new List<int>(2);
50 values.Add(iStart);
51 values.Add(iEnd);
52
53 condition.AddElement("IDN", values, ElementType.BetWeen);
54 this.Query(condition);
55 }
56
57 /// <summary>
58 /// 查詢記錄ID從X到Y并且編碼前1位為“X”的所有商品記錄。
59 /// </summary>
60 /// <param name="iStart">X</param>
61 /// <param name="iEnd">Y</param>
62 public void GetProductListByIDNAndCode(int iStart, int iEnd, string code)
63 {
64 Condition condition = this.CreateCondition();
65 List<int> values = new List<int>(2);
66 values.Add(iStart);
67 values.Add(iEnd);
68 condition.AddElement("IDN", values, ElementType.BetWeen);
69 condition.AddElement("SQL", "left(CODE,1)='" + code + "'", ElementType.SqlCondition);
70
71 this.Query(condition);
72 }
73
74 /// <summary>
75 /// 查詢記錄ID從X到Y并且編碼前12位為“Xx”的所有商品記錄。
76 /// </summary>
77 /// <param name="iStart">X</param>
78 /// <param name="iEnd">Y</param>
79 public void GetProductListByIDNOrCodeUnit(int iStart, int iEnd, string code, string unit)
80 {
81 Condition condition = this.CreateCondition();
82 List<int> values = new List<int>(2);
83 values.Add(iStart);
84 values.Add(iEnd);
85 condition.AddElement("IDN", values, ElementType.BetWeen);
86
87 Condition condition2 = this.CreateCondition();
88 condition2.AddElement("CODE", code, ElementType.MatchPrefix);
89 condition2.AddElement("UNIT", unit);
90
91 condition.AddCondition(condition2, ElementCombineType.Or);
92
93 this.Query(condition);
94 }
我們在控制台示範項目中使用這些查詢
1 class ConditionDemo
2 {
3 private void OutPut(ProductList table)
4 {
5 foreach (Product product in table.Rows)
6 {
7 System.Console.WriteLine(string.Format("Idn={0}\tCode={1}\tName={2}\tSpec={3}\tUnit={4}\tDescription={5}", product.Idn, product.Code, product.Name, product.Spec, product.Unit, product.Description));
8 }
9 }
10
11 /// <summary>
12 /// 查詢機關為“1*瓶/瓶”的所有商品,并且按商品編碼排序,示範EqualTo條件與排序條件。
13 /// </summary>
14 public void DemoCondition1()
15 {
16 ProductList table = new ProductList();
17 table.OrmAccessor = OrmContext.OrmAccessor;
18 table.GetProductListByUnit("1*瓶/瓶");
19 this.OutPut(table);
20 }
21
22 /// <summary>
23 /// 查詢商品編碼前1位為“1”的所有商品,并且按商品編碼倒序排序。示範SQL條件的使用。
24 /// </summary>
25 public void DemoCondition2()
26 {
27 ProductList table = new ProductList();
28 table.OrmAccessor = OrmContext.OrmAccessor;
29 table.GetProductListByCode1("1");
30 this.OutPut(table);
31 }
32
33 /// <summary>
34 /// 查詢商品編碼前2位為“1a”的所有商品,不處理排序。示範Like條件的使用。
35 /// </summary>
36 public void DemoCondition3()
37 {
38 ProductList table = new ProductList();
39 table.OrmAccessor = OrmContext.OrmAccessor;
40 table.GetProductListByCode2("1a");
41 this.OutPut(table);
42 }
43
44 /// <summary>
45 /// 查詢記錄ID從100到500的記錄,不處理排序。示範BetWeen條件的使用。
46 /// </summary>
47 public void DemoCondition4()
48 {
49 ProductList table = new ProductList();
50 table.OrmAccessor = OrmContext.OrmAccessor;
51 table.GetProductListByIDN(100, 500);
52 this.OutPut(table);
53 }
54
55 /// <summary>
56 /// 查詢記錄ID從100到500并且編碼前1位為“1”的所有商品記錄,不處理排序。示範兩個條件元素And組合的使用。
57 /// </summary>
58 public void DemoCondition5()
59 {
60 ProductList table = new ProductList();
61 table.OrmAccessor = OrmContext.OrmAccessor;
62 table.GetProductListByIDNAndCode(100, 500, "1");
63 this.OutPut(table);
64 }
65
66 /// <summary>
67 /// 查詢記錄ID從100到500并且編碼前1位為“1”的所有商品記錄以及商品編碼前2位為“1a”并且機關為“1*瓶/瓶”的商品記錄,不處理排序。示範兩個條件Or組合的使用。
68 /// </summary>
69 public void DemoCondition6()
70 {
71 ProductList table = new ProductList();
72 table.OrmAccessor = OrmContext.OrmAccessor;
73 table.GetProductListByIDNOrCodeUnit(100, 500, "1a", "1*瓶/瓶");
74 this.OutPut(table);
75 }
76 }
案例輸出結果:
<a href="http://images.cnblogs.com/cnblogs_com/eastjade/WindowsLiveWriter/AgileEAS.NETORM_13576/image_6.png"></a>
連結
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/19/1830812.html">一步一步教你使用AgileEAS.NET基礎類庫進行應用開發-系列目錄</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/12/1824405.html">AgileEAS.NET平台開發指南-系列目錄</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/09/1822530.html">AgileEAS.NET應用開發平台介紹-文章索引</a>
<a href="http://www.cnblogs.com/eastjade/archive/2010/09/15/1826870.html">AgileEAS.NET平台應用開發教程-案例計劃</a>
<a href="http://www.smarteas.net/">AgileEAS.NET官方網站</a>
<a href="http://www.agilelab.cn/">靈活軟體工程實驗室</a>
QQ群:116773358
本文轉自 agilelab 51CTO部落格,原文連結:http://blog.51cto.com/agilelab/603020