1

I am using the linqtotwitter function below to get statuses by ID and it works as expected. But I was wondering if there was a way to only call this function once(by passing the collection of id's to the query and the function would return a collection). Because the way I am currently getting statuses by looping through the collection of Id's and call GetTweetByKey each time.

C#:

public async Task<Status> GetTweetByKey(string key, StatusType statusType)
{
    try
    {
        return await (
        from tweet in _twitterContext.Status
        where tweet.Type == statusType &&
              tweet.ID == Convert.ToUInt64(key) &&
              tweet.TweetMode == TweetMode.Extended &&
              tweet.IncludeEntities == true
        select tweet).FirstOrDefaultAsync();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
Dev
  • 1,780
  • 3
  • 18
  • 46
  • Did my answer help you with the issue? If so, please mark it as accepted. If not, let me know and I'll try to help. – OfirD Oct 16 '18 at 19:19

1 Answers1

1

There are several ways to do it, the best would probably be using join. (This seems to get nowhere, because if any ID is not found, an exception is thrown, so that no tweet can be returned).

Try wrapping your original code with:

public async Task<List<Status>> GetTweetByKeys(string[] keys, StatusType statusType)
{
    var tasks = keys.Select(key => GetTweetByKey(key, statusType)).ToArray();
    var results = await Task.WhenAll(tasks);
    return results.ToList();
}
public async Task<Status> GetTweetByKey(string key, StatusType statusType)
{
    try
    {
        return await (
        from tweet in _twitterContext.Status
        where tweet.Type == statusType &&
              tweet.ID == Convert.ToUInt64(key) &&
              tweet.TweetMode == TweetMode.Extended &&
              tweet.IncludeEntities == true
        select tweet).FirstOrDefaultAsync();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}
OfirD
  • 9,442
  • 5
  • 47
  • 90
  • I have tried, and the problem I am trying to fix now is if the Tweet Id does not exist it goes to my catch block. I just want to get Tweet that exit and return it – Dev Oct 22 '18 at 20:28
  • What's the exception message? Does it happen also with your original code? – OfirD Oct 22 '18 at 20:40
  • `{LinqToTwitter.TwitterQueryException: No status found with that ID. at LinqToTwitter.Net.TwitterErrorHandler.BuildAndThrowTwitterQueryException(String responseStr, HttpResponseMessage msg) at LinqToTwitter.Net.TwitterErrorHandler.HandleGenericErrorAsync(HttpResponseMessage msg) at LinqToTwitter.Net.TwitterErrorHandler.ThrowIfErrorAsync(HttpResponseMessage msg)` – Dev Oct 22 '18 at 20:54
  • `Yes I was getting the exception in the original code but it was ok because if the tweet does not exist I would create a new instance of my tweet object which represent not found. with the original code if one tweet no longer exist it does not matter because it gets handled on a higher level where has in the answer if one does not exit I get none because I get the exception – Dev Oct 22 '18 at 20:54
  • I see. Well, It seems that you'll have to iterate each key and await all calls. I edited my answer to show how to do it. – OfirD Oct 22 '18 at 22:41