11using System ;
22using System . Collections . Generic ;
33using System . Data ;
4+ using System . Dynamic ;
45using System . Linq ;
56using Dapper ;
67
@@ -14,11 +15,52 @@ public static IEnumerable<T> Get<T>(this QueryFactory db, Query query)
1415 {
1516 var compiled = db . Compile ( query ) ;
1617
17- return db . Connection . Query < T > (
18+ var result = db . Connection . Query < T > (
1819 compiled . Sql ,
1920 compiled . NamedBindings ,
2021 commandTimeout : db . QueryTimeout
21- ) ;
22+ ) . ToList ( ) ;
23+
24+ if ( ! result . Any ( ) )
25+ {
26+ return result ;
27+ }
28+
29+ if ( result [ 0 ] is IDynamicMetaObjectProvider )
30+ {
31+ var dynamicResult = result
32+ . Cast < IDictionary < string , object > > ( )
33+ . Select ( x => new Dictionary < string , object > ( x , StringComparer . OrdinalIgnoreCase ) )
34+ . ToList ( ) ;
35+
36+ foreach ( var include in query . Includes )
37+ {
38+ var ids = dynamicResult . Where ( x => x [ include . ForeignKey ] != null )
39+ . Select ( x => x [ include . ForeignKey ] . ToString ( ) )
40+ . ToList ( ) ;
41+
42+ if ( ! ids . Any ( ) )
43+ {
44+ continue ;
45+ }
46+
47+ var related = include . Query . WhereIn ( include . LocalKey , ids ) . Get ( )
48+ . Cast < IDictionary < string , object > > ( )
49+ . Select ( x => new Dictionary < string , object > ( x , StringComparer . OrdinalIgnoreCase ) )
50+ . ToDictionary ( x => x [ include . LocalKey ] . ToString ( ) ) ;
51+
52+ foreach ( var item in dynamicResult )
53+ {
54+ var foreignValue = item [ include . ForeignKey ] . ToString ( ) ;
55+ item [ include . Name ] = related . ContainsKey ( foreignValue ) ? related [ foreignValue ] : null ;
56+ }
57+ }
58+
59+ return dynamicResult . Cast < T > ( ) ;
60+
61+ }
62+
63+ return result ;
2264 }
2365
2466 public static IEnumerable < IDictionary < string , object > > GetDictionary ( this QueryFactory db , Query query )
@@ -30,7 +72,7 @@ public static IEnumerable<IDictionary<string, object>> GetDictionary(this QueryF
3072
3173 public static IEnumerable < dynamic > Get ( this QueryFactory db , Query query )
3274 {
33- return Get < dynamic > ( db , query ) ;
75+ return Get < dynamic > ( db , query ) . Cast < IDictionary < string , object > > ( ) . ToList ( ) ;
3476 }
3577
3678 public static T First < T > ( this QueryFactory db , Query query )
@@ -42,7 +84,14 @@ public static T First<T>(this QueryFactory db, Query query)
4284
4385 public static dynamic First ( this QueryFactory db , Query query )
4486 {
45- return First < dynamic > ( db , query ) ;
87+ var list = Get < dynamic > ( db , query ) ;
88+
89+ if ( ! list . Any ( ) )
90+ {
91+ throw new InvalidOperationException ( "The sequence contains no elements" ) ;
92+ }
93+
94+ return list . ElementAt ( 0 ) ;
4695 }
4796
4897 public static T FirstOrDefault < T > ( this QueryFactory db , Query query )
@@ -54,7 +103,9 @@ public static T FirstOrDefault<T>(this QueryFactory db, Query query)
54103
55104 public static dynamic FirstOrDefault ( this QueryFactory db , Query query )
56105 {
57- return FirstOrDefault < dynamic > ( db , query ) ;
106+ var list = Get < dynamic > ( db , query ) ;
107+
108+ return list . Any ( ) ? list . ElementAt ( 0 ) : null ;
58109 }
59110
60111 public static int Execute (
0 commit comments