System.Threading.Tasks.Task`1[System.String] ?

Jun 28, 2014 at 8:13 PM
I'm trying to work with linq to twitter in an asp.net project, and I've done exactly this:

https://linqtotwitter.codeplex.com/wikipage?title=Single%20User%20Authorization

but all that returns is System.Threading.Tasks.Task`1[System.String]. Any ideas on what I'm doing wrong?

This is what my method looks like:
async Task<string> getTweet()
        {
            TwitterContext twitterCtx = new TwitterContext(auth);

            var searchResponse =
               await
               (from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == "\"LINQ to Twitter\""
                select search)
               .SingleOrDefaultAsync();

            if (searchResponse != null && searchResponse.Statuses != null)
                searchResponse.Statuses.ForEach(tweet =>
                    Console.WriteLine(
                        "User: {0}, Tweet: {1}",
                        tweet.User.ScreenNameResponse,
                        tweet.Text));

            return searchResponse.ToString();
        }
Coordinator
Jun 28, 2014 at 9:18 PM
Hi Jennifer,

The default implementation of Object.ToString() is to return the fully qualified type name as a string. The LINQ to Twitter Search class doesn't override ToString, so you get the Object implementation. In your code, searchResponse is a LINQ to Twitter Search class. Normally the object type would be "LinqToTwitter.Search". However, since your method is async and you're returning the ToString of the object, what you're actualling getting back is the fully qualified name of the return type "System.Threading.Tasks.Task1[System.String]". The "1[System.String]" syntax really means "<string>", which matches your method return type Task<string>.

Your code calls Console.WriteLine, which won't work in an ASP.NET application since you have no console to write to. Such is the problem with copy and pasting code because you need to read the code to ensure it works properly in your application. However, notice that the searchResponse has a Statuses collection that ForEach operates on. It executes a lambda for each tweet (Status class instance) in Statuses. If you chose to, you could extract the parts of the Status that you wanted to return to the calling code for processing. Otherwise, you could also return searchResponse or SearchResponse.Statuses. It's all a design decision that you can make, depending on what you need your application to do.

The searchResponse is a single instance, but as mentioned earlier, it contains a Statuses collection property that returns all of the tweets you're searching for. Logically, having a method named GetTweet that only returns a single tweet, yet is implemented to return multiple tweets might not make sense. There are a couple ways to fix this: if you only want one tweet, then change the logic to return only a single tweet (e.g. the first tweet). If you want multiple tweets, then change the return type to Task<List<Status>> or Task<List<string>> and then return, respectively, either searchResponses.Statuses or use something like the ForEach to build a List<string> that contains the Text property of each Status.

The downloadable source code (Source tab above) contains samples for various technologies. Check out either Linq2TwitterDemos_WebForms or Linq2TwitterDemos_Mvc, depending on which flavor of ASP.NET you're using.

@JoeMayo