Querying NuGet via LINQPad

24 February 2011 · 2 minute read · nuget

I was reading a blog post by Phil Haack today on How to find out which NuGet packages depend on yours and I decided I wanted to do a bit more digging into what I can find out about a package using NuGet’s OData feed.

A cool feature of LINQPad is that it supports OData feeds, so you can add any OData feed and query against it.

Well, NuGet is providing all its data via OData, so can we query it?

Sure!

NuGet in LINQPad

Let’s revisit the idea that Phil was talking about, finding out what packages depend on another one. Well since it’s just LINQ it’s really easy:

Querying NuGet

That’s all very cool but I decided to dig a bit deeper, I decided to do a simple bit of reporting, basically I’m interested to know this: What packages depend on package X and what version?.

I decided to use Autofac as a baseline since I know it’s got a number of versions released on NuGet.

Well it’s easy:

string packageName = "Autofac";

var dependencies = Packages
    .Where(x => x.Dependencies.Contains(packageName))
    .ToList()
    .Select(x => new {
        Package = x,
        Dependencies = x.Dependencies.Split('|').Select(y => new {
            Name = y.Split(':')[0],
            Version = y.Split(':')[1]
        })
    })
    .GroupBy(x => x.Dependencies.Where(y => y.Name == packageName).Select(y => y.Version).First())
    .OrderBy(x => x.Key.ToString());

dependencies.Dump();

Note: This is an OData feed so you’re limited to what queries are able to be done on the server. I’m not an OData expert (or really an OData user) so I’m doing most of it in memory once it’s returned.

This will then generate you a report which you can browse to find out what packages depend on what version.

If you’re interested, at time of writing here is the stats:

Autofac dependency stats

Looks like there’s quite a spread of what packages are depending on the Autofac versions ;).


Published: 2018-05-29 16:49:01 +1000 +1000, Version: aaf7f73