@@ -45,9 +45,7 @@ private Customer AddCustomerWithOrders()
45
45
Name = "Customer 1" ,
46
46
PrimaryAddress = new CustomerAddress
47
47
{
48
- AddressLine1 = "1 Humpty Street" ,
49
- City = "Humpty Doo" ,
50
- State = "Northern Territory" ,
48
+ AddressLine1 = "1 Australia Street" ,
51
49
Country = "Australia"
52
50
} ,
53
51
Orders = new [ ]
@@ -62,7 +60,7 @@ private Customer AddCustomerWithOrders()
62
60
return customer ;
63
61
}
64
62
65
- public class CustomerJoin
63
+ public class FullCustomerInfo
66
64
{
67
65
public int Id { get ; set ; }
68
66
public string Name { get ; set ; }
@@ -78,7 +76,7 @@ public void Can_do_multiple_joins_with_SqlExpression()
78
76
{
79
77
AddCustomerWithOrders ( ) ;
80
78
81
- var results = db . Select < CustomerJoin , Customer > ( q => q
79
+ var results = db . Select < FullCustomerInfo , Customer > ( q => q
82
80
. Join < Customer , CustomerAddress > ( )
83
81
. Join < Customer , Order > ( ) ) ;
84
82
@@ -89,7 +87,7 @@ public void Can_do_multiple_joins_with_SqlExpression()
89
87
. Join < Customer , CustomerAddress > ( )
90
88
. Join < Customer , Order > ( ) ;
91
89
92
- results = db . Select < CustomerJoin > ( expr ) ;
90
+ results = db . Select < FullCustomerInfo > ( expr ) ;
93
91
94
92
costs = results . ConvertAll ( x => x . Cost ) ;
95
93
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m , 2.99m } ) ) ;
@@ -100,7 +98,7 @@ public void Can_do_joins_with_wheres_using_SqlExpression()
100
98
{
101
99
AddCustomerWithOrders ( ) ;
102
100
103
- var results = db . Select < CustomerJoin , Customer > ( q => q
101
+ var results = db . Select < FullCustomerInfo , Customer > ( q => q
104
102
. Join < Customer , CustomerAddress > ( )
105
103
. Join < Customer , Order > ( ( c , o ) => c . Id == o . CustomerId && o . Cost < 2 ) ) ;
106
104
@@ -116,15 +114,15 @@ public void Can_do_joins_with_wheres_using_SqlExpression()
116
114
costs = orders . ConvertAll ( x => x . Cost ) ;
117
115
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m } ) ) ;
118
116
119
- results = db . Select < CustomerJoin , Customer > ( q => q
117
+ results = db . Select < FullCustomerInfo , Customer > ( q => q
120
118
. Join < Customer , CustomerAddress > ( )
121
119
. Join < Customer , Order > ( )
122
120
. Where < Order > ( o => o . Cost < 2 ) ) ;
123
121
124
122
costs = results . ConvertAll ( x => x . Cost ) ;
125
123
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m } ) ) ;
126
124
127
- results = db . Select < CustomerJoin , Customer > ( q => q
125
+ results = db . Select < FullCustomerInfo , Customer > ( q => q
128
126
. Join < Customer , CustomerAddress > ( )
129
127
. Join < Customer , Order > ( )
130
128
. Where < Order > ( o => o . Cost < 2 || o . LineItem == "Line 2" ) ) ;
@@ -136,7 +134,7 @@ public void Can_do_joins_with_wheres_using_SqlExpression()
136
134
. Join < Customer , CustomerAddress > ( )
137
135
. Join < Customer , Order > ( )
138
136
. Where < Order > ( o => o . Cost < 2 || o . LineItem == "Line 2" ) ;
139
- results = db . Select < CustomerJoin > ( expr ) ;
137
+ results = db . Select < FullCustomerInfo > ( expr ) ;
140
138
141
139
costs = results . ConvertAll ( x => x . Cost ) ;
142
140
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m , 2.99m } ) ) ;
@@ -145,61 +143,65 @@ public void Can_do_joins_with_wheres_using_SqlExpression()
145
143
[ Test ]
146
144
public void Can_do_joins_with_complex_wheres_using_SqlExpression ( )
147
145
{
148
- var customer1 = new Customer
146
+ var customers = new [ ]
149
147
{
150
- Name = "Customer 1" ,
151
- PrimaryAddress = new CustomerAddress
148
+ new Customer
152
149
{
153
- AddressLine1 = "1 Humpty Street" ,
154
- City = "Humpty Doo" ,
155
- State = "Northern Territory" ,
156
- Country = "Australia"
150
+ Name = "Customer 1" ,
151
+ PrimaryAddress = new CustomerAddress {
152
+ AddressLine1 = "1 Australia Street" ,
153
+ Country = "Australia"
154
+ } ,
155
+ Orders = new [ ] {
156
+ new Order { LineItem = "Line 1" , Qty = 1 , Cost = 1.99m } ,
157
+ new Order { LineItem = "Line 1" , Qty = 2 , Cost = 3.98m } ,
158
+ new Order { LineItem = "Line 2" , Qty = 1 , Cost = 1.49m } ,
159
+ new Order { LineItem = "Line 2" , Qty = 2 , Cost = 2.98m } ,
160
+ new Order { LineItem = "Australia Flag" , Qty = 1 , Cost = 9.99m } ,
161
+ } . ToList ( ) ,
157
162
} ,
158
- Orders = new [ ]
159
- {
160
- new Order { LineItem = "Line 1" , Qty = 1 , Cost = 1.99m } ,
161
- new Order { LineItem = "Line 1" , Qty = 2 , Cost = 3.98m } ,
162
- new Order { LineItem = "Line 2" , Qty = 1 , Cost = 1.49m } ,
163
- new Order { LineItem = "Line 2" , Qty = 2 , Cost = 2.98m } ,
164
- new Order { LineItem = "Australia Flag" , Qty = 1 , Cost = 9.99m } ,
165
- } . ToList ( ) ,
166
- } ;
167
-
168
- db . Save ( customer1 , references : true ) ;
169
-
170
- var customer2 = new Customer
171
- {
172
- Name = "Customer 2" ,
173
- PrimaryAddress = new CustomerAddress
163
+ new Customer
174
164
{
175
- AddressLine1 = "2 Prospect Park" ,
176
- City = "Brooklyn" ,
177
- State = "New York" ,
178
- Country = "USA"
165
+ Name = "Customer 2" ,
166
+ PrimaryAddress = new CustomerAddress {
167
+ AddressLine1 = "2 Prospect Park" ,
168
+ Country = "USA"
169
+ } ,
170
+ Orders = new [ ] {
171
+ new Order { LineItem = "USA" , Qty = 1 , Cost = 20m } ,
172
+ } . ToList ( ) ,
179
173
} ,
180
- Orders = new [ ]
181
- {
182
- new Order { LineItem = "USA" , Qty = 1 , Cost = 20m } ,
183
- } . ToList ( ) ,
184
174
} ;
185
175
186
- db . Save ( customer2 , references : true ) ;
176
+ customers . Each ( c =>
177
+ db . Save ( c , references : true ) ) ;
187
178
188
179
db . Insert (
189
180
new Country { CountryName = "Australia" , CountryCode = "AU" } ,
190
181
new Country { CountryName = "USA" , CountryCode = "US" } ) ;
191
182
192
- var results = db . Select < CustomerJoin , Customer > ( q => q
193
- . Join < Customer , CustomerAddress > ( )
194
- . Join < Customer , Order > ( )
183
+ var results = db . Select < FullCustomerInfo , Customer > ( q => q
184
+ . Join < CustomerAddress > ( ) //implicit
185
+ . Join < Customer , Order > ( ) //explicit
195
186
. Where ( c => c . Name == "Customer 1" )
196
187
. And < Order > ( o => o . Cost < 2 )
197
188
. Or < Order > ( o => o . LineItem == "Australia Flag" ) ) ;
198
189
199
190
var costs = results . ConvertAll ( x => x . Cost ) ;
200
191
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m , 1.49m , 9.99m } ) ) ;
201
192
202
- results = db . Select < CustomerJoin , Customer > ( q => q
193
+ //Same as above using using db.From<Customer>()
194
+ results = db . Select < FullCustomerInfo > ( db . From < Customer > ( )
195
+ . Join < CustomerAddress > ( ) //implicit
196
+ . Join < Customer , Order > ( ) //explicit
197
+ . Where ( c => c . Name == "Customer 1" )
198
+ . And < Order > ( o => o . Cost < 2 )
199
+ . Or < Order > ( o => o . LineItem == "Australia Flag" ) ) ;
200
+
201
+ costs = results . ConvertAll ( x => x . Cost ) ;
202
+ Assert . That ( costs , Is . EquivalentTo ( new [ ] { 1.99m , 1.49m , 9.99m } ) ) ;
203
+
204
+ results = db . Select < FullCustomerInfo , Customer > ( q => q
203
205
. Join < Customer , CustomerAddress > ( )
204
206
. Join < Customer , Order > ( )
205
207
. Where ( c => c . Name == "Customer 2" )
@@ -208,9 +210,9 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
208
210
costs = results . ConvertAll ( x => x . Cost ) ;
209
211
Assert . That ( costs , Is . EquivalentTo ( new [ ] { 20m } ) ) ;
210
212
211
- var countryResults = db . Select < CustomerJoin > ( db . From < Customer > ( )
212
- . Join < Order > ( ( c , o ) => c . Id == o . CustomerId ) //explicit join condition
213
+ var countryResults = db . Select < FullCustomerInfo > ( db . From < Customer > ( )
213
214
. Join < CustomerAddress > ( ) //implicit join with Customer
215
+ . Join < Order > ( ( c , o ) => c . Id == o . CustomerId ) //explicit join condition
214
216
. Join < CustomerAddress , Country > ( ( ca , c ) => ca . Country == c . CountryName )
215
217
. Where ( c => c . Name == "Customer 2" ) //implicit condition with Customer
216
218
. And < CustomerAddress , Order > ( ( a , o ) => a . Country == o . LineItem ) ) ;
@@ -230,41 +232,32 @@ public void Can_do_LeftJoins_using_SqlExpression()
230
232
new Customer
231
233
{
232
234
Name = "Customer 1" ,
233
- PrimaryAddress = new CustomerAddress
234
- {
235
- AddressLine1 = "1 Humpty Street" ,
236
- City = "Humpty Doo" ,
237
- State = "Northern Territory" ,
235
+ PrimaryAddress = new CustomerAddress {
236
+ AddressLine1 = "1 Australia Street" ,
238
237
Country = "Australia"
239
238
} ,
240
239
} ,
241
240
new Customer
242
241
{
243
242
Name = "Customer 2" ,
244
- PrimaryAddress = new CustomerAddress
245
- {
246
- AddressLine1 = "2 Humpty Street" ,
247
- City = "Humpty Doo" ,
248
- State = "Northern Territory" ,
243
+ PrimaryAddress = new CustomerAddress {
244
+ AddressLine1 = "2 America Street" ,
249
245
Country = "USA"
250
246
} ,
251
247
} ,
252
248
new Customer
253
249
{
254
250
Name = "Customer 3" ,
255
- PrimaryAddress = new CustomerAddress
256
- {
257
- AddressLine1 = "3 Humpty Street" ,
258
- City = "Humpty Doo" ,
259
- State = "Northern Territory" ,
251
+ PrimaryAddress = new CustomerAddress {
252
+ AddressLine1 = "3 Canada Street" ,
260
253
Country = "Canada"
261
254
} ,
262
255
} ,
263
256
} ;
264
257
265
258
customers . Each ( c =>
266
259
db . Save ( c , references : true ) ) ;
267
-
260
+
268
261
db . Insert (
269
262
new Country { CountryName = "Australia" , CountryCode = "AU" } ,
270
263
new Country { CountryName = "USA" , CountryCode = "US" } ,
0 commit comments