@@ -106,31 +106,14 @@ private static Result<MemberExpression> BuildPropertyPath(
106106 PropertyMappingTree propertyMappingTree
107107 )
108108 {
109- var current = startExpression ;
110- var currentMappingTree = propertyMappingTree ;
111-
112- foreach (
113- var ( segment , isLast ) in propertyPath . Segments . Select (
114- ( s , i ) => ( s , i == propertyPath . Segments . Count - 1 )
115- )
116- )
117- {
118- if ( ! currentMappingTree . TryGetProperty ( segment , out var propertyNode ) )
119- return Result . Fail ( $ "Invalid property '{ segment } ' in path") ;
120-
121- current = Expression . Property ( current , propertyNode . ActualPropertyName ) ;
122-
123- // Navigate to nested mapping for next segment
124- if ( ! isLast )
125- {
126- if ( ! propertyNode . HasNestedMapping )
127- return Result . Fail ( $ "Property '{ segment } ' does not support nested navigation") ;
128-
129- currentMappingTree = propertyNode . NestedMapping ;
130- }
131- }
132-
133- return Result . Ok ( ( MemberExpression ) current ) ;
109+ var result = propertyMappingTree . WalkPropertyPath (
110+ propertyPath . Segments ,
111+ startExpression ,
112+ "path"
113+ ) ;
114+ if ( result . IsFailed )
115+ return Result . Fail ( result . Errors ) ;
116+ return Result . Ok ( ( MemberExpression ) result . Value ) ;
134117 }
135118
136119 private static Result < MemberExpression > ResolvePropertyPathForCollection (
@@ -139,33 +122,14 @@ private static Result<MemberExpression> ResolvePropertyPathForCollection(
139122 PropertyMappingTree propertyMappingTree
140123 )
141124 {
142- var current = baseExpression ;
143- var currentMappingTree = propertyMappingTree ;
144-
145- for ( int i = 0 ; i < propertyPath . Segments . Count ; i ++ )
146- {
147- var segment = propertyPath . Segments [ i ] ;
148-
149- if ( ! currentMappingTree . TryGetProperty ( segment , out var propertyNode ) )
150- return Result . Fail (
151- $ "Invalid property '{ segment } ' in lambda expression property path"
152- ) ;
153-
154- current = Expression . Property ( current , propertyNode . ActualPropertyName ) ;
155-
156- // Navigate to nested mapping for next segment
157- if ( i < propertyPath . Segments . Count - 1 )
158- {
159- if ( ! propertyNode . HasNestedMapping )
160- return Result . Fail (
161- $ "Property '{ segment } ' does not support nested navigation in lambda expression"
162- ) ;
163-
164- currentMappingTree = propertyNode . NestedMapping ;
165- }
166- }
167-
168- return ( MemberExpression ) current ;
125+ var result = propertyMappingTree . WalkPropertyPath (
126+ propertyPath . Segments ,
127+ baseExpression ,
128+ "lambda expression property path"
129+ ) ;
130+ if ( result . IsFailed )
131+ return Result . Fail ( result . Errors ) ;
132+ return Result . Ok ( ( MemberExpression ) result . Value ) ;
169133 }
170134
171135 private static bool IsPrimitiveType ( Type type )
@@ -390,14 +354,14 @@ bool isEqual
390354 {
391355 var expressionToLower = Expression . Call ( expression , StringToLowerMethod ) ;
392356 var valueToLower = Expression . Call ( value , StringToLowerMethod ) ;
393- var nullCheck = Expression . NotEqual (
394- expression ,
395- Expression . Constant ( null , typeof ( string ) )
396- ) ;
397357
398358 if ( isEqual )
399359 {
400360 // eq: (expression != null && expression.ToLower() == value.ToLower())
361+ var nullCheck = Expression . NotEqual (
362+ expression ,
363+ Expression . Constant ( null , typeof ( string ) )
364+ ) ;
401365 return Expression . AndAlso (
402366 nullCheck ,
403367 Expression . Equal ( expressionToLower , valueToLower )
@@ -777,29 +741,12 @@ private static Result<Expression> BuildLambdaPropertyPath(
777741 int maxPropertyMappingDepth
778742 )
779743 {
780- var current = startExpression ;
781- var currentMappingTree = PropertyMappingTreeBuilder . BuildMappingTree (
744+ var mappingTree = PropertyMappingTreeBuilder . BuildMappingTree (
782745 elementType ,
783746 maxPropertyMappingDepth
784747 ) ;
785748
786- foreach ( var segment in segments )
787- {
788- if ( ! currentMappingTree . TryGetProperty ( segment , out var propertyNode ) )
789- {
790- return Result . Fail ( $ "Invalid property '{ segment } ' in lambda property path") ;
791- }
792-
793- current = Expression . Property ( current , propertyNode . ActualPropertyName ) ;
794-
795- // Update mapping tree for nested navigation
796- if ( propertyNode . HasNestedMapping )
797- {
798- currentMappingTree = propertyNode . NestedMapping ;
799- }
800- }
801-
802- return Result . Ok ( current ) ;
749+ return mappingTree . WalkPropertyPath ( segments , startExpression , "lambda property path" ) ;
803750 }
804751
805752 private static Type GetCollectionElementType ( Type collectionType )
0 commit comments