e0x3
e0x3

Reputation: 131

linqtotwitter get tweets without video

I am trying to get tweets by using linqtotwitter with c# and there is a task to get 10 last tweets without video content. firstly I have retrieved last 10 statuses like this:

var srch = Enumerable.SingleOrDefault((from search in
                                    twitterContext.Search
                                        where search.Type == SearchType.Search &&
                                           search.Query == hashTag &&
                                           search.Count == 10
                                        select search));

and secondly I am trying to exclude tweets with video content:

    var result = srch.Statuses.ToList()
                  .Where(item => item.Entities.MediaEntities.
                            Where(innerItems => innerItems.VideoInfo.Duration == 0)); // shows error

status containsentities collection, where each entity contains mediaentities collection, where each mediaEntity has videoInfo property. But there is some difficulty to compose a correct linq query to a such complex structure like statuses collection.

Upvotes: 1

Views: 127

Answers (1)

TheRock
TheRock

Reputation: 1541

I think you just need to familiarize yourself with linq. Nevertheless here's what you're probably looking for:

var srch = twitterContext.Search
            .FirstOrDefault(search => 
                search.Type == SearchType.Search && 
                search.Query == hashTag);

var srchWithoutVideo = srch?.Statuses?.Where(status =>
                status.Entities.MediaEntities.All(entity => entity.VideoInfo.Duration == 0))
            .OrderByDescending(status => status.CreatedAt)
            .Take(10);

I'm guessing at your intended result - Move the count out of the search. I think you may want all the tweets first by the hashTag and then in the second query you can filter, order by CreatedAt Descending and take 10.

Upvotes: 1

Related Questions