@@ -535,9 +535,17 @@ private Dictionary<string, string> ListToDictionary(List<DimensionSet> dimension
535535 var dictionary = new Dictionary < string , string > ( ) ;
536536 try
537537 {
538- return dimensions != null
539- ? new Dictionary < string , string > ( dimensions . SelectMany ( x => x . Dimensions ) )
540- : dictionary ;
538+ if ( dimensions != null )
539+ {
540+ foreach ( var dimensionSet in dimensions )
541+ {
542+ foreach ( var kvp in dimensionSet . Dimensions )
543+ {
544+ dictionary [ kvp . Key ] = kvp . Value ;
545+ }
546+ }
547+ }
548+ return dictionary ;
541549 }
542550 catch ( Exception e )
543551 {
@@ -633,20 +641,34 @@ private static MetricDefinition GetExistingMetric(List<MetricDefinition> metrics
633641 {
634642 // Use a traditional for loop instead of LINQ to avoid enumeration issues
635643 // when the collection is modified concurrently
636- for ( int i = 0 ; i < metrics . Count ; i ++ )
644+ if ( metrics == null || string . IsNullOrEmpty ( key ) )
645+ return null ;
646+
647+ // Create a snapshot of the count to avoid issues with concurrent modifications
648+ var count = metrics . Count ;
649+ for ( int i = 0 ; i < count ; i ++ )
637650 {
638651 try
639652 {
653+ // Check bounds again in case collection was modified
654+ if ( i >= metrics . Count )
655+ break ;
656+
640657 var metric = metrics [ i ] ;
641- if ( metric ? . Name == key )
658+ if ( metric != null && string . Equals ( metric . Name , key , StringComparison . Ordinal ) )
642659 {
643660 return metric ;
644661 }
645662 }
646663 catch ( ArgumentOutOfRangeException )
647664 {
648665 // Collection was modified during iteration, return null to be safe
649- return null ;
666+ break ;
667+ }
668+ catch ( IndexOutOfRangeException )
669+ {
670+ // Collection was modified during iteration, return null to be safe
671+ break ;
650672 }
651673 }
652674 return null ;
0 commit comments