@@ -292,5 +292,75 @@ public void Can_do_LeftJoins_using_SqlExpression()
292292 Assert . That ( dbAddresses . Count , Is . EqualTo ( 3 ) ) ;
293293 }
294294
295+ [ Test ]
296+ public void Can_Join_on_matching_Alias_convention ( )
297+ {
298+ db . DropAndCreateTable < AliasedCustomer > ( ) ;
299+ db . DropAndCreateTable < AliasedCustomerAddress > ( ) ;
300+
301+ var customers = new [ ]
302+ {
303+ new AliasedCustomer
304+ {
305+ Name = "Customer 1" ,
306+ PrimaryAddress = new AliasedCustomerAddress {
307+ AddressLine1 = "1 Australia Street" ,
308+ Country = "Australia"
309+ } ,
310+ } ,
311+ new AliasedCustomer
312+ {
313+ Name = "Customer 2" ,
314+ PrimaryAddress = new AliasedCustomerAddress {
315+ AddressLine1 = "2 America Street" ,
316+ Country = "USA"
317+ } ,
318+ } ,
319+ new AliasedCustomer
320+ {
321+ Name = "Customer 3" ,
322+ PrimaryAddress = new AliasedCustomerAddress {
323+ AddressLine1 = "3 Canada Street" ,
324+ Country = "Canada"
325+ } ,
326+ } ,
327+ } ;
328+
329+ customers . Each ( c =>
330+ db . Save ( c , references : true ) ) ;
331+
332+ db . Insert (
333+ new Country { CountryName = "Australia" , CountryCode = "AU" } ,
334+ new Country { CountryName = "USA" , CountryCode = "US" } ,
335+ new Country { CountryName = "Italy" , CountryCode = "IT" } ,
336+ new Country { CountryName = "Spain" , CountryCode = "ED" } ) ;
337+
338+ //Normal Join
339+ var dbCustomers = db . Select < AliasedCustomer > ( q => q
340+ . Join < AliasedCustomerAddress > ( )
341+ . Join < AliasedCustomerAddress , Country > ( ( ca , c ) => ca . Country == c . CountryName ) ) ;
342+
343+ Assert . That ( dbCustomers . Count , Is . EqualTo ( 2 ) ) ;
344+
345+ //Left Join
346+ dbCustomers = db . Select < AliasedCustomer > ( q => q
347+ . Join < AliasedCustomerAddress > ( )
348+ . LeftJoin < AliasedCustomerAddress , Country > ( ( ca , c ) => ca . Country == c . CountryName ) ) ;
349+
350+ Assert . That ( dbCustomers . Count , Is . EqualTo ( 3 ) ) ;
351+
352+ //Warning: Right and Full Joins are not implemented by Sqlite3. Avoid if possible.
353+ var dbCountries = db . Select < Country > ( q => q
354+ . LeftJoin < AliasedCustomerAddress > ( ( c , ca ) => ca . Country == c . CountryName )
355+ . LeftJoin < AliasedCustomerAddress , AliasedCustomer > ( ) ) ;
356+
357+ Assert . That ( dbCountries . Count , Is . EqualTo ( 4 ) ) ;
358+
359+ var dbAddresses = db . Select < AliasedCustomerAddress > ( q => q
360+ . LeftJoin < Country > ( ( ca , c ) => ca . Country == c . CountryName )
361+ . LeftJoin < AliasedCustomerAddress , AliasedCustomer > ( ) ) ;
362+
363+ Assert . That ( dbAddresses . Count , Is . EqualTo ( 3 ) ) ;
364+ }
295365 }
296366}
0 commit comments