NuGet.PackageReferenceFile.FindEntryWithLatestVersionById C# (CSharp) Method

FindEntryWithLatestVersionById() public method

public FindEntryWithLatestVersionById ( string id ) : PackageName
id string
return PackageName
        public PackageName FindEntryWithLatestVersionById(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "id");
            }

            XDocument document = GetDocument();
            if (document == null)
            {
                return null;
            }

            return (from e in document.Root.Elements("package")
                    let entryId = e.GetOptionalAttributeValue("id")
                    let entryVersion = SemanticVersion.ParseOptionalVersion(e.GetOptionalAttributeValue("version"))
                    where entryId != null && entryVersion != null
                    where id.Equals(entryId, StringComparison.OrdinalIgnoreCase)
                    orderby entryVersion descending
                    select new PackageName(entryId, entryVersion)).FirstOrDefault();
        }

Usage Example

        public bool TryFindLatestPackageById(string id, out SemanticVersion latestVersion)
        {
            PackageName packageName = _packageReferenceFile.FindEntryWithLatestVersionById(id);

            if (packageName == null)
            {
                latestVersion = null;
                return(false);
            }
            else
            {
                latestVersion = packageName.Version;
                Debug.Assert(latestVersion != null);
                return(true);
            }
        }