This project has moved and is read-only. For the latest updates, please go here.

How to get n tweets by screenname, excluding retweets

Feb 18, 2011 at 3:57 PM
Edited Feb 19, 2011 at 5:43 PM

I've used Linq2Twitter a lot, but have not yet found a nice nice way of getting a specified number of tweets for a specified user, excluding retweets.

The following code returns no tweets, for my screenName="timacheson" and retweetCount=15, because I have too many retweets!

How can I adapt my code so it always returns 15 tweets, regardless of how many retweets I have?

public List<Status> GetRetweets(string screenName, int retweetCount)
    var statusRetweets =
            (from tweet in new TwitterContext().Status
            where tweet.Type == StatusType.User &&
                  tweet.ScreenName == screenName && 
                  tweet.IncludeRetweets == false &&
                  tweet.Count == tweetCount
            select tweet);

    return statusRetweets.ToList();
Feb 19, 2011 at 5:20 PM

Hi Tim,

I'm not seeing a straight forward solution for this with the Twitter API.  The only possible approaches that I'm seeing right now are to either request more tweets than necessary and then use LINQ to Objects to trim the list and/or find a good average or median count and loop on subsequent queries until you get that number.  I'm seeing the drawbacks to this approach as I think of it (i.e. people who retweet a lot) so there might have to be some type of limits to how many requests you'll perform; otherwise, you'll eat through your API request limit in no time.

Have you thought about streams?  LINQ to Twitter supports the streaming API now.


Feb 19, 2011 at 5:34 PM

Hi Joe, I agree, I cant find a neat solution. It's a shame there isn't a better way in the Twitter API. The solution I'm using is indeed to retrieve more tweets than necessary, but of course it's a shame to do that and the results are unpredictable. I haven't found an algorythm that achieves consistent and reliable results.

I have not yet explored the streaming API. Do you think that could offer a better solution to this problem? Can you point me in the right direction in terms of how to achieve this with Linq2Twitter?


Feb 20, 2011 at 4:47 AM

The main problems you face with using API requests are either exceeding API limits or requesting too much data that burns CPU cycles and bandwidth, which is what made me think about streams.  How you use streams will depend on what you're trying to do.  My first thoughts are to look at User and/or Site streams.  The User stream is limited in the number of connections and users you can have.  Site streams are still in beta, and you'll have to get permission to join the beta, but will offer the ability to get data for more users.  The stream will return data to your application as it becomes available.  So, it's running continuously.  You connect, specifying a callback handler, and the handler will save, display, or perform some other processing on the content. Each invocation of the callback runs on a separate thread (avoids blocking the LINQ to Twitter stream listener), so you'll need to marshal back to the UI thread if you want to display results.  I think streams is something to look at, but since I'm not familiar with your particular use case or what resources you have to devote toward writing the code to support streams, you'll probably need to weigh the pros and cons.

The interface is a little more raw than the API calls (i.e. you have to deserialize your own JSON) and I don't have any documentation on this yet.  However, there are open source libraries for working with JSON and you can get started by reading the streaming concepts docs at  I have example code in the downloadable source code that you can try out to get a feel for how LINQ to Twitter supports streams.  I'll be doing another release soon because of some bugs that have cropped up.  If you decide that streams is something that will work for you, I'd be interested in hearing how it goes.