LINQ to Twitter Queries with LINQPad

Posted by Joe Mayo on Geeks with Blogs See other posts from Geeks with Blogs or by Joe Mayo
Published on Thu, 24 Feb 2011 18:31:55 GMT Indexed on 2011/02/24 23:26 UTC
Read the original article Hit count: 435

Filed under:

LINQPad is a popular utility for .NET developers who use LINQ a lot.  In addition to standard SQL queries, LINQPad also supports other types of LINQ providers, including LINQ to Twitter.  The following sections explain how to set up LINQPad for making queries with LINQ to Twitter.

LINQPad comes in a couple versions and this example uses LINQPad4, which runs on the .NET Framework 4.0.

1. The first thing you'll need to do is set up a reference to the LinqToTwitter.dll. From the Query menu, select query properties. Click the Browse button and find the LinqToTwitter.dll binary. You should see something similar to the Query Properties window below.

LINQPad References Page

2. While you have the query properties window open, add the namespace for the LINQ to Twitter types.  Click the Additional Namespace Imports tab and type in LinqToTwitter. The results are shown below:

LINQPad Namespaces Page

3. The default query type, when you first start LINQPad, is C# Expression, but you'll need to change this to support multiple statements.  Change the Language dropdown, on the Main window, to C# Statements.

4. To query LINQ to Twitter, instantiate a TwitterContext, by typing the following into the LINQPad Query window:

var ctx = new TwitterContext();

Note: If you're getting syntax errors, go back and make sure you did steps #2 and #3 properly.

5. Next, add a query, but don't materialize it, like this:

var tweets =
	from tweet in ctx.Status
	where tweet.Type == StatusType.Public
	select new
	{
		tweet.Text,
		tweet.Geo,
		tweet.User
	};

6. Next, you want the output to be displayed in the LINQPad grid, so do a Dump, like this:

tweets.Dump();

The following image shows the final results:

 LINQ to Twitter Query in LINQPad

That was an unauthenticated query, but you can also perform authenticated queries with LINQ to Twitter's support of OAuth.  Here's an example that uses the PinAuthorizer (type this into the LINQPad Query window):

var auth = new PinAuthorizer
{
	Credentials = new InMemoryCredentials
	{
		ConsumerKey = "",
		ConsumerSecret = ""
	},
	UseCompression = true,
	GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
	GetPin = () =>
	{
		// this executes after user authorizes, which begins with the call to auth.Authorize() below.
		Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n");
		Console.Write("Enter the PIN number here: ");
		return Console.ReadLine();
	}
};

// start the authorization process (launches Twitter authorization page).
auth.Authorize();

var ctx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/");

var tweets =
	from tweet in ctx.Status
	where tweet.Type == StatusType.Public
	select new
	{
		tweet.Text,
		tweet.Geo,
		tweet.User
	};

tweets.Dump();

This code is very similar to what you'll find in the LINQ to Twitter downloadable source code solution, in the LinqToTwitterDemo project.  For obvious reasons, I changed the value assigned to ConsumerKey and ConsumerSecret, which you'll have to obtain by visiting http://dev.twitter.com and registering your application.

One tip, you'll probably want to make this easier on yourself by creating your own DLL that encapsulates all of the OAuth logic and then call a method or property on you custom class that returns a fully functioning TwitterContext.  This will help avoid adding all this code every time you want to make a query.

Now, you know how to set up LINQPad for LINQ to Twitter, perform unauthenticated queries, and perform queries with OAuth.

Joe

© Geeks with Blogs or respective owner