Querying NuGet via LINQPad

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 ;).

nuget
Posted by: Aaron Powell
Last revised: 25 Feb, 2011 05:44 AM History

Comments

04 May, 2011 02:04 PM

I received an error when running the query - InvalidOperationException, Sequence contains no elements.

Removing the GroupBy and OrderBy displayed the ungrouped list (44 items).

Nice query, anyhow!

Thanks, Gary

Daniel Williams
Daniel Williams
01 Apr, 2011 04:16 PM

Nice! Thanks for the intro to LinqPad as well. What a handy tool.

blog comments powered by Disqus