@@ -49,21 +49,21 @@ static Expression<Func<TEntity, TEntity>> BuildExpression<TEntity>(
4949 foreach ( var ( navFieldName , navProjection ) in projection . Navigations )
5050 {
5151 var prop = GetProperty ( entityType , navFieldName ) ;
52- if ( prop != null && addedProperties . Add ( prop . Name ) )
52+ if ( prop == null ||
53+ ! addedProperties . Add ( prop . Name ) )
5354 {
54- var binding = BuildNavigationBinding ( parameter , prop , navProjection , keyNames ) ;
55- if ( binding != null )
56- {
57- bindings . Add ( binding ) ;
58- }
55+ continue ;
5956 }
57+
58+ var binding = BuildNavigationBinding ( parameter , prop , navProjection , keyNames ) ;
59+ bindings . Add ( binding ) ;
6060 }
6161
6262 var memberInit = Expression . MemberInit ( Expression . New ( entityType ) , bindings ) ;
6363 return Expression . Lambda < Func < TEntity , TEntity > > ( memberInit , parameter ) ;
6464 }
6565
66- static MemberBinding ? BuildNavigationBinding (
66+ static MemberBinding BuildNavigationBinding (
6767 ParameterExpression parameter ,
6868 PropertyInfo prop ,
6969 NavigationProjectionInfo navProjection ,
@@ -72,7 +72,7 @@ static Expression<Func<TEntity, TEntity>> BuildExpression<TEntity>(
7272 ? BuildCollectionNavigationBinding ( parameter , prop , navProjection , keyNames )
7373 : BuildSingleNavigationBinding ( parameter , prop , navProjection , keyNames ) ;
7474
75- static MemberBinding BuildCollectionNavigationBinding (
75+ static MemberAssignment BuildCollectionNavigationBinding (
7676 ParameterExpression parameter ,
7777 PropertyInfo prop ,
7878 NavigationProjectionInfo navProjection ,
@@ -102,8 +102,8 @@ static MemberBinding BuildCollectionNavigationBinding(
102102
103103 var orderByMethod = typeof ( Enumerable )
104104 . GetMethods ( BindingFlags . Static | BindingFlags . Public )
105- . First ( m => m . Name == "OrderBy" &&
106- m . GetParameters ( ) . Length == 2 )
105+ . First ( _ => _ . Name == "OrderBy" &&
106+ _ . GetParameters ( ) . Length == 2 )
107107 . MakeGenericMethod ( navType , keyProp . PropertyType ) ;
108108
109109 orderedCollection = Expression . Call ( null , orderByMethod , navAccess , keyLambda ) ;
@@ -113,9 +113,9 @@ static MemberBinding BuildCollectionNavigationBinding(
113113 // Build: x.Children.OrderBy(...).Select(n => new Child { ... })
114114 var selectMethod = typeof ( Enumerable )
115115 . GetMethods ( BindingFlags . Static | BindingFlags . Public )
116- . First ( m => m . Name == "Select" &&
117- m . GetParameters ( ) . Length == 2 &&
118- m . GetParameters ( ) [ 1 ] . ParameterType . GetGenericArguments ( ) . Length == 2 )
116+ . First ( _ => _ . Name == "Select" &&
117+ _ . GetParameters ( ) . Length == 2 &&
118+ _ . GetParameters ( ) [ 1 ] . ParameterType . GetGenericArguments ( ) . Length == 2 )
119119 . MakeGenericMethod ( navType , navType ) ;
120120
121121 var selectCall = Expression . Call ( null , selectMethod , orderedCollection , innerLambda ) ;
@@ -193,17 +193,14 @@ static List<MemberBinding> BuildNavigationBindings(
193193 if ( prop != null && addedProperties . Add ( prop . Name ) )
194194 {
195195 var binding = BuildNestedNavigationBinding ( sourceExpression , prop , nestedNavProjection , keyNames ) ;
196- if ( binding != null )
197- {
198- bindings . Add ( binding ) ;
199- }
196+ bindings . Add ( binding ) ;
200197 }
201198 }
202199
203200 return bindings ;
204201 }
205202
206- static MemberBinding ? BuildNestedNavigationBinding (
203+ static MemberAssignment BuildNestedNavigationBinding (
207204 Expression sourceExpression ,
208205 PropertyInfo prop ,
209206 NavigationProjectionInfo navProjection ,
@@ -235,8 +232,8 @@ static List<MemberBinding> BuildNavigationBindings(
235232
236233 var orderByMethod = typeof ( Enumerable )
237234 . GetMethods ( BindingFlags . Static | BindingFlags . Public )
238- . First ( m => m . Name == "OrderBy" &&
239- m . GetParameters ( ) . Length == 2 )
235+ . First ( _ => _ . Name == "OrderBy" &&
236+ _ . GetParameters ( ) . Length == 2 )
240237 . MakeGenericMethod ( navType , keyProp . PropertyType ) ;
241238
242239 orderedCollection = Expression . Call ( null , orderByMethod , navAccess , keyLambda ) ;
@@ -246,9 +243,9 @@ static List<MemberBinding> BuildNavigationBindings(
246243 // .Select(n => new Child { ... })
247244 var selectMethod = typeof ( Enumerable )
248245 . GetMethods ( BindingFlags . Static | BindingFlags . Public )
249- . First ( m => m . Name == "Select" &&
250- m . GetParameters ( ) . Length == 2 &&
251- m . GetParameters ( ) [ 1 ] . ParameterType . GetGenericArguments ( ) . Length == 2 )
246+ . First ( _ => _ . Name == "Select" &&
247+ _ . GetParameters ( ) . Length == 2 &&
248+ _ . GetParameters ( ) [ 1 ] . ParameterType . GetGenericArguments ( ) . Length == 2 )
252249 . MakeGenericMethod ( navType , navType ) ;
253250
254251 var selectCall = Expression . Call ( null , selectMethod , orderedCollection , innerLambda ) ;
@@ -289,31 +286,33 @@ static List<MemberBinding> BuildNavigationBindings(
289286
290287 static string BuildCacheKey < TEntity > ( FieldProjectionInfo projection )
291288 {
292- var sb = new StringBuilder ( ) ;
293- sb . Append ( typeof ( TEntity ) . FullName ) ;
294- sb . Append ( '|' ) ;
295- BuildProjectionKey ( sb , projection ) ;
296- return sb . ToString ( ) ;
289+ var builder = new StringBuilder ( ) ;
290+ builder . Append ( typeof ( TEntity ) . FullName ) ;
291+ builder . Append ( '|' ) ;
292+ BuildProjectionKey ( builder , projection ) ;
293+ return builder . ToString ( ) ;
297294 }
298295
299- static void BuildProjectionKey ( StringBuilder sb , FieldProjectionInfo projection )
296+ static void BuildProjectionKey ( StringBuilder builder , FieldProjectionInfo projection )
300297 {
301298 // Sort scalar fields for consistent cache key
302299 var sortedScalars = projection . ScalarFields . OrderBy ( _ => _ , StringComparer . OrdinalIgnoreCase ) ;
303- sb . Append ( string . Join ( "," , sortedScalars ) ) ;
300+ builder . Append ( string . Join ( ',' , sortedScalars ) ) ;
304301
305- if ( projection . Navigations . Count > 0 )
302+ if ( projection . Navigations . Count <= 0 )
306303 {
307- sb . Append ( '{' ) ;
308- var sortedNavs = projection . Navigations . OrderBy ( kv => kv . Key , StringComparer . OrdinalIgnoreCase ) ;
309- foreach ( var ( navName , navProjection ) in sortedNavs )
310- {
311- sb . Append ( navName ) ;
312- sb . Append ( ':' ) ;
313- BuildProjectionKey ( sb , navProjection . Projection ) ;
314- sb . Append ( ';' ) ;
315- }
316- sb . Append ( '}' ) ;
304+ return ;
305+ }
306+
307+ builder . Append ( '{' ) ;
308+ var sortedNavs = projection . Navigations . OrderBy ( _ => _ . Key , StringComparer . OrdinalIgnoreCase ) ;
309+ foreach ( var ( navName , navProjection ) in sortedNavs )
310+ {
311+ builder . Append ( navName ) ;
312+ builder . Append ( ':' ) ;
313+ BuildProjectionKey ( builder , navProjection . Projection ) ;
314+ builder . Append ( ';' ) ;
317315 }
316+ builder . Append ( '}' ) ;
318317 }
319318}
0 commit comments