Search on Windows PHone

Feb 24, 2013 at 8:44 PM
Hi,

I'm trying to get a generic search working on Windows Phone. I downloaded the WP7TweetWithMediaDemo and got that working fine. I then tried to implement search and added the following method.
      public static void PerformSearch()
        {
            var twitterCtx = new TwitterContext();

            var queryResults =
                from search in twitterCtx.Search
                where search.Type == SearchType.Search &&
                      search.Query == "Linq To Twitter"
                select search;

            Search srch = queryResults.SingleOrDefault();

            Debug.WriteLine("\nQuery: {0}\n", srch.QueryResult);
            srch.Results.ForEach(entry =>
                Debug.WriteLine(
                    "ID: {0, -15}, Source: {1}\nContent: {2}\n",
                    entry.ID, entry.Source, entry.Text));
        }
The search results are always null. What am I missing?
Thanks for your help,
Mike.
Coordinator
Feb 25, 2013 at 9:29 AM
Hi,

On Windows Phone (or any Silverlight app), you should use async queries. The downloadable source code has demos and you can use the MaterializedAsyncCallback, like this:
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == "LINQ To Twitter"
             select search)
            .MaterializedAsyncCallback(resp =>
            {
                if (resp.Status != TwitterErrorStatus.Success)
                {
                    Exception ex = resp.Exception;
                    // handle error
                    throw ex;
                }

                Search srch = resp.State.First();
                Console.WriteLine("\nQuery: {0}\n", srch.SearchMetaData.Query);

                srch.Statuses.ForEach(entry =>
                    Console.WriteLine(
                        "ID: {0, -15}, Source: {1}\nContent: {2}\n",
                        entry.StatusID, entry.Source, entry.Text));

                Console.WriteLine("\n More Search demos can be downloaded from LINQ to Twitter's on-line samples at http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20Samples&referringTitle=Home");
            });
@JoeMayo