@@ -50,6 +50,14 @@ public class Order
5050 public decimal Cost { get ; set ; }
5151 }
5252
53+ public class Country
54+ {
55+ [ AutoIncrement ]
56+ public int Id { get ; set ; }
57+ public string CountryName { get ; set ; }
58+ public string CountryCode { get ; set ; }
59+ }
60+
5361 /// <summary>
5462 /// Test POCOs using table aliases and an alias on the foreign key reference
5563 /// </summary>
@@ -143,6 +151,7 @@ public class LoadReferencesTests
143151 db . DropAndCreateTable < Order > ( ) ;
144152 db . DropAndCreateTable < Customer > ( ) ;
145153 db . DropAndCreateTable < CustomerAddress > ( ) ;
154+ db . DropAndCreateTable < Country > ( ) ;
146155 db . DropAndCreateTable < AliasedCustomer > ( ) ;
147156 db . DropAndCreateTable < AliasedCustomerAddress > ( ) ;
148157 db . DropAndCreateTable < OldAliasedCustomer > ( ) ;
@@ -366,6 +375,7 @@ public class CustomerJoin
366375 public string City { get ; set ; }
367376 public string LineItem { get ; set ; }
368377 public decimal Cost { get ; set ; }
378+ public string CountryCode { get ; set ; }
369379 }
370380
371381 [ Test ]
@@ -461,7 +471,7 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
461471 } ;
462472
463473 db . Save ( customer1 , references : true ) ;
464-
474+
465475 var customer2 = new Customer
466476 {
467477 Name = "Customer 2" ,
@@ -480,6 +490,10 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
480490
481491 db . Save ( customer2 , references : true ) ;
482492
493+ db . Insert (
494+ new Country { CountryName = "Australia" , CountryCode = "AU" } ,
495+ new Country { CountryName = "USA" , CountryCode = "US" } ) ;
496+
483497 var results = db . Select < CustomerJoin , Customer > ( q => q
484498 . Join < Customer , CustomerAddress > ( )
485499 . Join < Customer , Order > ( )
@@ -496,12 +510,23 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
496510 . Where ( c => c . Name == "Customer 2" )
497511 . And < CustomerAddress , Order > ( ( a , o ) => a . Country == o . LineItem ) ) ;
498512
513+ costs = results . ConvertAll ( x => x . Cost ) ;
514+ Assert . That ( costs , Is . EquivalentTo ( new [ ] { 20m } ) ) ;
515+
516+ var countryResults = db . Select < CustomerJoin > ( db . From < Customer > ( )
517+ . Join < Order > ( ( c , o ) => c . Id == o . CustomerId ) //explicit join condition
518+ . Join < CustomerAddress > ( ) //implicit join with Customer
519+ . Join < CustomerAddress , Country > ( ( ca , c ) => ca . Country == c . CountryName )
520+ . Where ( c => c . Name == "Customer 2" ) //implicit condition with Customer
521+ . And < CustomerAddress , Order > ( ( a , o ) => a . Country == o . LineItem ) ) ;
522+
499523 db . GetLastSql ( ) . Print ( ) ;
500524
501- costs = results . ConvertAll ( x => x . Cost ) ;
525+ costs = countryResults . ConvertAll ( x => x . Cost ) ;
502526 Assert . That ( costs , Is . EquivalentTo ( new [ ] { 20m } ) ) ;
527+ Assert . That ( countryResults . ConvertAll ( x => x . CountryCode ) , Is . EquivalentTo ( new [ ] { "US" } ) ) ;
503528 }
504-
529+
505530 }
506531
507532}
0 commit comments