StreetFoo.Client.ReportItem.GetSearchSuggestionsAsync C# (CSharp) Method

GetSearchSuggestionsAsync() public static method

public static GetSearchSuggestionsAsync ( string queryText ) : List>>.Task
queryText string
return List>>.Task
        public static async Task<Dictionary<string, List<ReportItem>>> GetSearchSuggestionsAsync(string queryText)
        {
            // get everything and sort by the title...
            var conn = StreetFooRuntime.GetUserDatabase();
            var reports = await conn.QueryAsync<ReportItem>("select * from ReportItem where title like ? order by title",
                new object[] { queryText + "%" });

            // walk and build a distinct list of matches...
            var results = new Dictionary<string, List<ReportItem>>(StringComparer.CurrentCultureIgnoreCase);
            foreach (var report in reports)
            {
                // if we don't have a result with that title...
                if (!(results.ContainsKey(report.Title)))
                    results[report.Title] = new List<ReportItem>();

                // add...
                results[report.Title].Add(report);
            }

            // return...
            return results;
        }

Usage Example

Example #1
0
        public static async Task PopulateSuggestionsAsync(string queryText, SearchSuggestionCollection results)
        {
            // if we don't have at least three characters to work with, do nothing...
            if (queryText.Length < 3)
            {
                return;
            }

            // how many?
            int maxSuggestions = 5;

            // get the list...
            var suggestions = await ReportItem.GetSearchSuggestionsAsync(queryText);

            // sort the suggestions...
            var titles = new List <string>();

            foreach (var title in suggestions.Keys)
            {
                titles.Add(title);
            }
            titles.Sort();

            // do we have one that we can use as a recommendation?
            ReportItem recommendation = null;

            foreach (var title in titles)
            {
                if (suggestions[title].Count == 1)
                {
                    recommendation = suggestions[title][0];
                    break;
                }
            }

            // if we have a recommendation only show three suggestions...
            if (recommendation != null)
            {
                maxSuggestions -= 2;
            }

            // add the suggestions...
            foreach (var title in titles)
            {
                results.AppendQuerySuggestion(title);

                // enough?
                if (results.Size == maxSuggestions)
                {
                    break;
                }
            }

            // add the recommendation...
            if (recommendation != null)
            {
                // we need an image...
                var viewItem = new ReportViewItem(recommendation);
                var imageUri = await new ReportImageCacheManager().GetLocalImageUriAsync(viewItem);

                // add the suggestion...
                results.AppendSearchSeparator("Recommendation");
                results.AppendResultSuggestion(recommendation.Title, recommendation.Description, recommendation.Id.ToString(),
                                               RandomAccessStreamReference.CreateFromUri(new Uri(imageUri)), recommendation.Title);
            }
        }