Search code examples
twitterlinq-to-twitter

Exclude Retweets using LinqToTwitter


My LinqToTwitter class can not exclude retweets.

var srch = (from search in twitterCtx.Search where search.Type == SearchType.Search && search.Query == term && search.Count == 100 select search).SingleOrDefault();

There is no option about search.IncludeRetweets==false.

How can I do a search with that? Should I try another class?


Solution

  • The Twitter API doesn't offer that option, so neither does LINQ to Twitter. That said, here's what you can do:

    1. Perform your search query as normal. It will contain retweets.
    2. Perform a LINQ to Objects query on the results using a where clause that sets the condition of RetweetedStatus.StatusID == 0, like this:

      var nonRetweetedStatuses =
          (from tweet in searchResponse.Statuses
           where tweet.RetweetedStatus.StatusID == 0
           select tweet)
          .ToList();
      

    The reason why I suggested using the where clause like this is because LINQ to Twitter instantiates a Status for RetweetedStatus, regardless of whether it exists or not. However, the contents of that Status are the default values of each properties type. Since StatusID will never be 0 for a valid retweet, this works. You could also filter on another field like Text == null and it would still work.