0
|
1 using System;
|
|
2 using System.Linq;
|
|
3 using System.Linq.Expressions;
|
|
4
|
|
5 using BLToolkit.Data.DataProvider;
|
|
6 using BLToolkit.Data.Linq;
|
|
7
|
|
8 using NUnit.Framework;
|
|
9
|
|
10 namespace Data.Linq
|
|
11 {
|
|
12 using Model;
|
|
13
|
|
14 [TestFixture]
|
|
15 public class ExpressionsTest : TestBase
|
|
16 {
|
|
17 static int Count1(Parent p) { return p.Children.Count(c => c.ChildID > 0); }
|
|
18
|
|
19 [Test]
|
|
20 public void MapMember1()
|
|
21 {
|
|
22 Expressions.MapMember<Parent,int>(p => Count1(p), p => p.Children.Count(c => c.ChildID > 0));
|
|
23
|
|
24 ForEachProvider(db => AreEqual(Parent.Select(p => Count1(p)), db.Parent.Select(p => Count1(p))));
|
|
25 }
|
|
26
|
|
27 static int Count2(Parent p, int id) { return p.Children.Count(c => c.ChildID > id); }
|
|
28
|
|
29 [Test]
|
|
30 public void MapMember2()
|
|
31 {
|
|
32 Expressions.MapMember<Parent,int,int>((p,id) => Count2(p, id), (p, id) => p.Children.Count(c => c.ChildID > id));
|
|
33
|
|
34 ForEachProvider(db => AreEqual(Parent.Select(p => Count2(p, 1)), db.Parent.Select(p => Count2(p, 1))));
|
|
35 }
|
|
36
|
|
37 static int Count3(Parent p, int id) { return p.Children.Count(c => c.ChildID > id) + 2; }
|
|
38
|
|
39 [Test]
|
|
40 public void MapMember3()
|
|
41 {
|
|
42 Expressions.MapMember<Parent,int,int>((p,id) => Count3(p, id), (p, id) => p.Children.Count(c => c.ChildID > id) + 2);
|
|
43
|
|
44 var n = 2;
|
|
45
|
|
46 ForEachProvider(new[] { ProviderName.SqlCe }, db => AreEqual(Parent.Select(p => Count3(p, n)), db.Parent.Select(p => Count3(p, n))));
|
|
47 }
|
|
48
|
|
49 [MethodExpression("Count4Expression")]
|
|
50 static int Count4(Parent p, int id, int n)
|
|
51 {
|
|
52 return (_count4Expression ?? (_count4Expression = Count4Expression().Compile()))(p, id, n);
|
|
53 }
|
|
54
|
|
55 static Func<Parent,int,int,int> _count4Expression;
|
|
56
|
|
57 static Expression<Func<Parent,int,int,int>> Count4Expression()
|
|
58 {
|
|
59 return (p, id, n) => p.Children.Count(c => c.ChildID > id) + n;
|
|
60 }
|
|
61
|
|
62 [Test]
|
|
63 public void MethodExpression4()
|
|
64 {
|
|
65 var n = 3;
|
|
66
|
|
67 ForEachProvider(db => AreEqual(
|
|
68 Parent.Select(p => Count4(p, n, 4)),
|
|
69 db.Parent.Select(p => Count4(p, n, 4))));
|
|
70 }
|
|
71
|
|
72 [MethodExpression("Count5Expression")]
|
|
73 static int Count5(ITestDataContext db, Parent p, int n)
|
|
74 {
|
|
75 return (_count5Expression ?? (_count5Expression = Count5Expression().Compile()))(db, p, n);
|
|
76 }
|
|
77
|
|
78 static Func<ITestDataContext,Parent,int,int> _count5Expression;
|
|
79
|
|
80 static Expression<Func<ITestDataContext,Parent,int,int>> Count5Expression()
|
|
81 {
|
|
82 return (db, p, n) => Sql.AsSql(db.Child.Where(c => c.ParentID == p.ParentID).Count() + n);
|
|
83 }
|
|
84
|
|
85 [Test]
|
|
86 public void MethodExpression5()
|
|
87 {
|
|
88 var n = 2;
|
|
89
|
|
90 ForEachProvider(new[] { ProviderName.SqlCe, ProviderName.Firebird }, db => AreEqual(
|
|
91 Parent.Select(p => Child.Where(c => c.ParentID == p.ParentID).Count() + n),
|
|
92 db.Parent.Select(p => Count5(db, p, n))));
|
|
93 }
|
|
94
|
|
95 [MethodExpression("Count6Expression")]
|
|
96 static int Count6(Table<Child> c, Parent p)
|
|
97 {
|
|
98 return (_count6Expression ?? (_count6Expression = Count6Expression().Compile()))(c, p);
|
|
99 }
|
|
100
|
|
101 static Func<Table<Child>,Parent,int> _count6Expression;
|
|
102
|
|
103 static Expression<Func<Table<Child>,Parent,int>> Count6Expression()
|
|
104 {
|
|
105 return (ch, p) => ch.Where(c => c.ParentID == p.ParentID).Count();
|
|
106 }
|
|
107
|
|
108 [Test]
|
|
109 public void MethodExpression6()
|
|
110 {
|
|
111 ForEachProvider(db => AreEqual(
|
|
112 Parent.Select(p => Child.Where(c => c.ParentID == p.ParentID).Count()),
|
|
113 db.Parent.Select(p => Count6(db.Child, p))));
|
|
114 }
|
|
115
|
|
116 [MethodExpression("Count7Expression")]
|
|
117 static int Count7(Table<Child> ch, Parent p, int n)
|
|
118 {
|
|
119 return (_count7Expression ?? (_count7Expression = Count7Expression().Compile()))(ch, p, n);
|
|
120 }
|
|
121
|
|
122 static Func<Table<Child>,Parent,int,int> _count7Expression;
|
|
123
|
|
124 static Expression<Func<Table<Child>,Parent,int,int>> Count7Expression()
|
|
125 {
|
|
126 return (ch, p, n) => Sql.AsSql(ch.Where(c => c.ParentID == p.ParentID).Count() + n);
|
|
127 }
|
|
128
|
|
129 [Test]
|
|
130 public void MethodExpression7()
|
|
131 {
|
|
132 var n = 2;
|
|
133
|
|
134 ForEachProvider(new[] { ProviderName.SqlCe, ProviderName.Firebird }, db => AreEqual(
|
|
135 Parent.Select(p => Child.Where(c => c.ParentID == p.ParentID).Count() + n),
|
|
136 db.Parent.Select(p => Count7(db.Child, p, n))));
|
|
137 }
|
|
138
|
|
139 [MethodExpression("Expression8")]
|
|
140 static IQueryable<Parent> GetParent(ITestDataContext db, Child ch)
|
|
141 {
|
|
142 throw new InvalidOperationException();
|
|
143 }
|
|
144
|
|
145 static Expression<Func<ITestDataContext,Child,IQueryable<Parent>>> Expression8()
|
|
146 {
|
|
147 return (db,ch) =>
|
|
148 from p in db.Parent
|
|
149 where p.ParentID == (int)Math.Floor(ch.ChildID / 10.0)
|
|
150 select p;
|
|
151 }
|
|
152
|
|
153 [Test]
|
|
154 public void MethodExpression8()
|
|
155 {
|
|
156 ForEachProvider(db => AreEqual(
|
|
157 from ch in Child
|
|
158 from p in
|
|
159 from p in Parent
|
|
160 where p.ParentID == ch.ChildID / 10
|
|
161 select p
|
|
162 where ch.ParentID == p.ParentID
|
|
163 select ch
|
|
164 ,
|
|
165 from ch in db.Child
|
|
166 from p in GetParent(db, ch)
|
|
167 where ch.ParentID == p.ParentID
|
|
168 select ch));
|
|
169 }
|
|
170
|
|
171 [Test]
|
|
172 public void MethodExpression9()
|
|
173 {
|
|
174 using (var db = new TestDbManager())
|
|
175 AreEqual(
|
|
176 from ch in Child
|
|
177 from p in
|
|
178 from p in Parent
|
|
179 where p.ParentID == ch.ChildID / 10
|
|
180 select p
|
|
181 where ch.ParentID == p.ParentID
|
|
182 select ch
|
|
183 ,
|
|
184 from ch in db.Child
|
|
185 from p in TestDbManager.GetParent9(db, ch)
|
|
186 where ch.ParentID == p.ParentID
|
|
187 select ch);
|
|
188 }
|
|
189
|
|
190 [Test]
|
|
191 public void MethodExpression10()
|
|
192 {
|
|
193 using (var db = new TestDbManager())
|
|
194 AreEqual(
|
|
195 from ch in Child
|
|
196 from p in
|
|
197 from p in Parent
|
|
198 where p.ParentID == ch.ChildID / 10
|
|
199 select p
|
|
200 where ch.ParentID == p.ParentID
|
|
201 select ch
|
|
202 ,
|
|
203 from ch in db.Child
|
|
204 from p in db.GetParent10(ch)
|
|
205 where ch.ParentID == p.ParentID
|
|
206 select ch);
|
|
207 }
|
|
208
|
|
209 [MethodExpression("GetBoolExpression1")]
|
|
210 static bool GetBool1<T>(T obj)
|
|
211 {
|
|
212 throw new InvalidOperationException();
|
|
213 }
|
|
214
|
|
215 static Expression<Func<T,bool>> GetBoolExpression1<T>()
|
|
216 where T : class
|
|
217 {
|
|
218 return obj => obj != null;
|
|
219 }
|
|
220
|
|
221 [Test]
|
|
222 public void TestGenerics1()
|
|
223 {
|
|
224 using (var db = new TestDbManager())
|
|
225 {
|
|
226 var q =
|
|
227 from ch in db.Child
|
|
228 where GetBool1(ch.Parent)
|
|
229 select ch;
|
|
230
|
|
231 q.ToList();
|
|
232 }
|
|
233 }
|
|
234
|
|
235 [MethodExpression("GetBoolExpression2_{0}")]
|
|
236 static bool GetBool2<T>(T obj)
|
|
237 {
|
|
238 throw new InvalidOperationException();
|
|
239 }
|
|
240
|
|
241 static Expression<Func<Parent,bool>> GetBoolExpression2_Parent()
|
|
242 {
|
|
243 return obj => obj != null;
|
|
244 }
|
|
245
|
|
246 [Test]
|
|
247 public void TestGenerics2()
|
|
248 {
|
|
249 using (var db = new TestDbManager())
|
|
250 {
|
|
251 var q =
|
|
252 from ch in db.Child
|
|
253 where GetBool2(ch.Parent)
|
|
254 select ch;
|
|
255
|
|
256 q.ToList();
|
|
257 }
|
|
258 }
|
|
259
|
|
260 class TestClass<T>
|
|
261 {
|
|
262 [MethodExpression("GetBoolExpression3")]
|
|
263 public static bool GetBool3(Parent obj)
|
|
264 {
|
|
265 throw new InvalidOperationException();
|
|
266 }
|
|
267
|
|
268 static Expression<Func<Parent,bool>> GetBoolExpression3()
|
|
269 {
|
|
270 return obj => obj != null;
|
|
271 }
|
|
272 }
|
|
273
|
|
274 [Test]
|
|
275 public void TestGenerics3()
|
|
276 {
|
|
277 using (var db = new TestDbManager())
|
|
278 {
|
|
279 var q =
|
|
280 from ch in db.Child
|
|
281 where TestClass<int>.GetBool3(ch.Parent)
|
|
282 select ch;
|
|
283
|
|
284 q.ToList();
|
|
285 }
|
|
286 }
|
|
287 }
|
|
288 }
|