protected ProjectId GetBestProjectId_NoLock <T>(
                    Dictionary <ProjectId, T> workQueue,
                    ProjectId?projectId,
                    ProjectDependencyGraph dependencyGraph,
                    IDiagnosticAnalyzerService?analyzerService
                    )
                {
                    if (projectId != null)
                    {
                        if (workQueue.ContainsKey(projectId))
                        {
                            return(projectId);
                        }

                        // prefer project that directly depends on the given project and has diagnostics as next project to
                        // process
                        foreach (
                            var dependingProjectId in dependencyGraph.GetProjectsThatDirectlyDependOnThisProject(
                                projectId
                                )
                            )
                        {
                            if (
                                workQueue.ContainsKey(dependingProjectId) &&
                                analyzerService?.ContainsDiagnostics(
                                    Workspace,
                                    dependingProjectId
                                    ) == true
                                )
                            {
                                return(dependingProjectId);
                            }
                        }
                    }

                    // prefer a project that has diagnostics as next project to process.
                    foreach (var pendingProjectId in workQueue.Keys)
                    {
                        if (
                            analyzerService?.ContainsDiagnostics(Workspace, pendingProjectId)
                            == true
                            )
                        {
                            return(pendingProjectId);
                        }
                    }

                    // explicitly iterate so that we can use struct enumerator
                    foreach (var pair in workQueue)
                    {
                        return(pair.Key);
                    }

                    throw ExceptionUtilities.Unreachable;
                }