@@ -627,6 +627,69 @@ public void Can_load_references_with_OrderBy()
627627 Assert . That ( orders . Count , Is . EqualTo ( 6 ) ) ;
628628 }
629629
630+ [ Test ]
631+ public void Can_load_select_with_join ( )
632+ {
633+ // Drop tables in order that FK allows
634+ db . DropTable < TABLE_3 > ( ) ;
635+ db . DropTable < TABLE_2 > ( ) ;
636+ db . DropTable < TABLE_1 > ( ) ;
637+ db . CreateTable < TABLE_1 > ( ) ;
638+ db . CreateTable < TABLE_2 > ( ) ;
639+ db . CreateTable < TABLE_3 > ( ) ;
640+
641+ var id1 = db . Insert ( new TABLE_1 { One = "A" } , selectIdentity : true ) ;
642+ var id2 = db . Insert ( new TABLE_1 { One = "B" } , selectIdentity : true ) ;
643+
644+ db . Insert ( new TABLE_2 { Three = "C" , TableOneKey = ( int ) id1 } ) ;
645+
646+ var q = db . From < TABLE_1 > ( )
647+ . Join < TABLE_2 > ( ) ;
648+ var results = db . LoadSelect ( q ) ;
649+
650+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
651+ Assert . That ( results [ 0 ] . One , Is . EqualTo ( "A" ) ) ;
652+
653+ var row3 = new TABLE_3
654+ {
655+ Three = "3a" ,
656+ TableTwo = new TABLE_2
657+ {
658+ Three = "3b" ,
659+ TableOneKey = ( int ) id1 ,
660+ }
661+ } ;
662+ db . Save ( row3 , references : true ) ;
663+
664+ Assert . That ( row3 . TableTwoKey , Is . EqualTo ( row3 . TableTwo . Id ) ) ;
665+
666+ row3 = db . LoadSingleById < TABLE_3 > ( row3 . Id ) ;
667+ Assert . That ( row3 . TableTwoKey , Is . EqualTo ( row3 . TableTwo . Id ) ) ;
668+ }
669+
670+ [ Test ]
671+ public void Can_load_select_with_join_and_same_name_columns ( )
672+ {
673+ // Drop tables in order that FK allows
674+ db . DropTable < ProjectTask > ( ) ;
675+ db . DropTable < Project > ( ) ;
676+ db . CreateTable < Project > ( ) ;
677+ db . CreateTable < ProjectTask > ( ) ;
678+
679+ db . Insert ( new Project { Val = "test" } ) ;
680+ db . Insert ( new ProjectTask { Val = "testTask" , ProjectId = 1 } ) ;
681+
682+ var query = db . From < ProjectTask > ( )
683+ . Join < ProjectTask , Project > ( ( pt , p ) => pt . ProjectId == p . Id ) ;
684+
685+ var selectResults = db . Select ( query ) ;
686+
687+ var results = db . LoadSelect ( query ) ;
688+
689+ Assert . That ( results . Count , Is . EqualTo ( 1 ) ) ;
690+ Assert . That ( results [ 0 ] . Val , Is . EqualTo ( "testTask" ) ) ;
691+ }
692+
630693 [ Test ]
631694 public void Can_load_references_with_OrderBy_and_Paging ( )
632695 {
@@ -727,4 +790,35 @@ public class TABLE_3 : IHasId<int>
727790 [ Reference ]
728791 public TABLE_2 TableTwo { get ; set ; }
729792 }
793+
794+ [ Schema ( "dbo" ) ]
795+ [ Alias ( "ProjectTask" ) ]
796+ public class ProjectTask : IHasId < int >
797+ {
798+ [ Alias ( "ProjectTaskId" ) ]
799+ [ Index ( Unique = true ) ]
800+ [ AutoIncrement ]
801+ public int Id { get ; set ; }
802+
803+ [ References ( typeof ( Project ) ) ]
804+ public int ProjectId { get ; set ; }
805+
806+ [ Reference ]
807+ public Project Project { get ; set ; }
808+
809+ public string Val { get ; set ; }
810+ }
811+
812+ [ Schema ( "dbo" ) ]
813+ [ Alias ( "Project" ) ]
814+ public class Project : IHasId < int >
815+ {
816+ [ Alias ( "ProjectId" ) ]
817+ [ Index ( Unique = true ) ]
818+ [ AutoIncrement ]
819+ public int Id { get ; set ; }
820+
821+ public string Val { get ; set ; }
822+
823+ }
730824}
0 commit comments