@@ -19,18 +19,22 @@ internal class RemoveProjectFromSolutionCommand : CommandBase
19
19
private readonly string _fileOrDirectory ;
20
20
private readonly IReadOnlyCollection < string > _projects ;
21
21
22
- private int CountNonFolderDescendants ( SolutionModel solution , SolutionFolderModel item , Dictionary < SolutionFolderModel , int > cached )
22
+ private int CountNonFolderDescendants (
23
+ SolutionModel solution ,
24
+ SolutionFolderModel item ,
25
+ Dictionary < SolutionFolderModel , SolutionItemModel [ ] > solutionItemsGroupedByParent ,
26
+ Dictionary < SolutionFolderModel , int > cached )
23
27
{
24
28
if ( cached . ContainsKey ( item ) )
25
29
{
26
30
return cached [ item ] ;
27
31
}
28
32
int count = item . Files ? . Count ?? 0 ;
29
- var children = solution . SolutionItems . Where ( i => i . Parent == item ) ;
33
+ var children = solutionItemsGroupedByParent . TryGetValue ( item , out var items ) ? items : Array . Empty < SolutionItemModel > ( ) ;
30
34
foreach ( var child in children )
31
35
{
32
36
count += child is SolutionFolderModel folderModel
33
- ? CountNonFolderDescendants ( solution , folderModel , cached )
37
+ ? CountNonFolderDescendants ( solution , folderModel , solutionItemsGroupedByParent , cached )
34
38
: 1 ;
35
39
}
36
40
cached . Add ( item , count ) ;
@@ -111,10 +115,15 @@ private async Task RemoveProjectsAsync(string solutionFileFullPath, IEnumerable<
111
115
}
112
116
}
113
117
118
+ Dictionary < SolutionFolderModel , SolutionItemModel [ ] > solutionItemsGroupedByParent = solution . SolutionItems
119
+ . Where ( i => i . Parent != null )
120
+ . GroupBy ( i => i . Parent )
121
+ . ToDictionary ( g => g . Key , g => g . ToArray ( ) ) ;
122
+
114
123
Dictionary < SolutionFolderModel , int > nonFolderDescendantsCount = new ( ) ;
115
124
foreach ( var item in solution . SolutionFolders )
116
125
{
117
- CountNonFolderDescendants ( solution , item , nonFolderDescendantsCount ) ;
126
+ CountNonFolderDescendants ( solution , item , solutionItemsGroupedByParent , nonFolderDescendantsCount ) ;
118
127
}
119
128
120
129
var emptyFolders = nonFolderDescendantsCount . Where ( i => i . Value == 0 ) . Select ( i => i . Key ) ;
0 commit comments