Ejemplo n.º 1
0
        /// <summary>
        /// Lists the questions via Ajax
        /// </summary>
        /// <param name="prefix">Prefix of the question's title which we look for in tags</param>
        /// <returns></returns>
        public ActionResult FindQuestion(string prefix)
        {
            Regex rgx = new Regex("[^a-zA-Z0-9 -]");

            //Remove the characters which can't included in the tags
            prefix = rgx.Replace(prefix, "");
            //Get the wordlist
            var wordlist = prefix.Split(' ');

            List <Question> quesList = null;

            foreach (var item in wordlist)
            {
                if (quesList != null)
                {
                    var temp = QuestionManager.AllQuestionToTag(item);
                    if (temp != null)
                    {
                        quesList = quesList.Concat(temp).ToList();
                    }
                }
                else
                {
                    quesList = QuestionManager.AllQuestionToTag(item);
                }
            }
            //Remove the duplications from the questions
            List <Question> questions = null;

            if (quesList != null)
            {
                questions = quesList.GroupBy(q => q.Id).Select(g => g.First()).ToList();
            }

            //Create a KeyValue list
            var allQues = new List <KeyValuePair <int, string> >();

            if (questions != null)
            {
                foreach (var item in questions)
                {
                    allQues.Add(new KeyValuePair <int, string>(item.Id, item.Title));
                }


                var result = (from l in allQues select l).ToList();

                result = (from l in allQues
                          orderby l.Value
                          select l).ToList();

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(null);
            }
        }