-2

I'm using TweetInvi to grab a bunch of tweets that match a specified hashtag. I do this with the following:

var matchingTweets = Search.SearchTweets(hashtag);

This returns an IEnumerable (named ITweet, interface of Tweet), however I cannot create a List<> of Tweets, because Tweet is a static type.

I made, instead, a list of objects, using:

List<object> matches = matchingTweets.Cast<object>().ToList();

However, although each member of the matchingTweets IEnumerable has a number of properties, I cannot access them using:

long tweetID = matches[i].<property>;

Using matches[i].ToString() returns the tweet content, so how can I effectively cast the results in matchingTweets to a list, and subsequently access the properties of those list members? I would ideally like to avoid using dynamic.

Wolfish
  • 960
  • 2
  • 8
  • 34

2 Answers2

1

It makes sense you cannot access the properties. You cast it into object so you can only access the objects properties and methods (that like you said might have been overridden).

It should be fine to just access it like this:

List<ITweet> tweets = matchingTweets.Take(5).ToList(); 

What you can do is project it to a new object of yours:

var tweets = matchingTweets.Select(item => new {
                                       property1 = item.property1,
                                       property2 = item.property2
                                   })
                           .Take(5).ToList();

Then you will be able to access what you need. Now, if you need to share this data outside the scope of that function create a DTO object and initialize it instead of the anonymous type.

Depending on the size of the project and amount of effort usually it is in any case a good practice to create a layer of DTO objects when you interact with an external service like this. Then if their models changed you can contain your changes only to the DTOs.


If all you want are the ids of the first 5 then:

var ids = matchingTweets.Take(5).Select(item => item.id).ToList();
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • @Wolfish - if you want to pull the results of the first 5 then check update – Gilad Green Mar 31 '17 at 18:22
  • Just to note - Your answer also works, but @maccettura provided an a answer within the context of TweetInvi, which was more relevant here, hence the selection of his answer as the "chosen" answer. – Wolfish Mar 31 '17 at 18:28
1

In your example above you were trying to grab the ID from the tweet. ITweet implements ITweetIdentifier which contains the Id property. You can literally just access it by:

var matchingTweets = Search.SearchTweets(hashtag);

//Grab the first 5 tweets from the results.
var firstFiveTweets = matchingTweets.Take(5).ToList();

//if you only want the ids and not the entire object
var firstFiveTweetIds = matchingTweets.Take(5).Select(t => t.Id).ToList();

//Iterate through and do stuff
foreach (var tweet in matchingTweets)
{
    //These are just examples of the properties accessible to you...
    if(tweet.Favorited)
    {
        var text = tweet.FullText;
    }     
    if(tweet.RetweetCount > 100)
    {
        //TODO: Handle popular tweets...
    }   
}

//Get item at specific index
matchingTweets.ElementAt(index);

I don't know exactly what you want to do with all the info, but since the SearchTweets returns a IEnumerable of ITweets you have access to anything an ITweet has defined.

I highly recommend looking through their wiki. It's pretty well organized and gives you clear examples of some basic tasks.

maccettura
  • 10,514
  • 3
  • 28
  • 35