Search code examples
c#twitterlinq-to-twitter

What is the better way to find tweet using LINQ to twitter?


I'm working with twitter in my project and I need to find tweet by status Id.

Here is an example of my code:

private Status GetStatusById(string Id, TwitterContext ctx)
{
    ulong longId = Convert.ToUInt64(Id);
    var response = ctx.Status.Where(t => t.Type == StatusType.User &&
                                        t.ID == Id).ToList();
    return response.FirstOrDefault(s => s.ID == Id);
}

After executing this method I have an Exception:

LinqToTwitter.TwitterQueryException: Sorry, that page does not exist

I'm sure that tweet really exist!

So I don't have my tweet in the query result and I need to do more queries until I will find it. But I want to do this by only ONE query!

I was using a search code like this

ulong longId = Convert.ToUInt64(Id);
var searchResults =
                (from search in ctx.Search
                 where search.Type == SearchType.Search &&
                 search.MaxID <= longId &&
                 search.SinceID >= longId
                 select search)
                .SingleOrDefault();
return searchResults.Statuses.FirstOrDefault(s => s.ID == Id);

But this query require searching text and I don't have it!

Am I missing something?

Is there any way to find tweet(status) only by ID using ONE query?


Solution

  • In this case, you would use a Status/Show query, like this in v2.1:

            var friendTweets =
                from tweet in twitterCtx.Status
                where tweet.Type == StatusType.Show &&
                      tweet.ID == Id
                select tweet;
    

    or this in v3.0:

            var friendTweets =
                await
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.Show &&
                       tweet.ID == Id
                 select tweet)
                .FirstOrDefaultAsync();
    

    The reason you received a Page does not exist because your query was for a user with that ID and that user doesn't exist. Here's the documentation for this query:

    http://linqtotwitter.codeplex.com/wikipage?title=Querying%20Statuses&referringTitle=Making%20Status%20Queries%20and%20Calls