private IEnumerable <Package> GetPackagesForOwners(IEnumerable <int> ownerKeys, bool includeUnlisted,
                                                           bool includeVersions, bool includeVulnerabilities)
        {
            IQueryable <Package> packages = _packageRepository.GetAll()
                                            .Where(p => p.PackageRegistration.Owners.Any(o => ownerKeys.Contains(o.Key)));

            if (!includeUnlisted)
            {
                packages = packages.Where(p => p.Listed);
            }

            if (!includeVersions)
            {
                // Do a best effort of retrieving the latest versions. Note that UpdateIsLatest has had concurrency issues
                // where sometimes packages no rows with IsLatest set. In this case, we'll just select the last inserted
                // row (descending [Key]) as opposed to reading all rows into memory and sorting on NuGetVersion.
                packages = packages
                           .GroupBy(p => p.PackageRegistrationKey)
                           .Select(g => g
                                   // order booleans desc so that true (1) comes first
                                   .OrderByDescending(p => p.IsLatestStableSemVer2)
                                   .ThenByDescending(p => p.IsLatestStable)
                                   .ThenByDescending(p => p.IsLatestSemVer2)
                                   .ThenByDescending(p => p.IsLatest)
                                   .ThenByDescending(p => p.Listed)
                                   .ThenByDescending(p => p.Key)
                                   .FirstOrDefault());
            }

            var result = packages
                         .Include(p => p.PackageRegistration)
                         .Include(p => p.PackageRegistration.Owners)
                         .Include(p => p.PackageRegistration.RequiredSigners);

            if (includeVulnerabilities && _featureFlagService.IsManagePackagesVulnerabilitiesEnabled())
            {
                result = result.Include($"{nameof(Package.VulnerablePackageRanges)}.{nameof(VulnerablePackageVersionRange.Vulnerability)}");
            }

            return(result.ToList());
        }