none
Sorting by position of string in .Contains() RRS feed

  • Question

  • I am developing a search form that uses a textbox with an ajax dropdown which finds keywords.  Currently, I am using Contains() to search for the keywords.  I can sort the results alphabetically like in the example below, but what I would really like to do is sort them by how close to the beginning of the keyword the string appears.

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
        {
            MyDataContext context = new MyDataContext();
            var keyords =
                from keyword in context.Keywords
                where keyword.KeywordName.Contains(prefixText)
                select keyword.KeywordName;
            keywords = keywords.OrderBy(keyword => keyword);
            return keywords.ToArray();
        }

    So if someone types in "Urology", the list will show "Neurology" first and "Urology" second since 'N' comes before 'U'.  Any ideas?
    Friday, April 24, 2009 3:54 PM

Answers