예제 #1
0
파일: PlaceDAO.cs 프로젝트: linhndg/HNHB
        public HNHB.Models.PlaceModels.SearchModel Search(HNHB.Models.PlaceModels.SearchModel search)
        {
            string tempKeyword = null;
            if (search.Keyword != null) tempKeyword = search.Keyword.ToUnsign();
            List<String> stringKeyword = new List<string>();
            int subIndex = -1;
            for (int i = 0; i < tempKeyword.Length; i++)
            {
                if (tempKeyword[i].Equals('\"'))
                {
                    if (subIndex < 0)
                    {
                        subIndex = i;
                    }
                    else
                    {
                        stringKeyword.Add(tempKeyword.Substring(subIndex + 1, i - subIndex - 1).Trim().ToUnsign().ToLower());
                        tempKeyword = tempKeyword.Remove(subIndex, i - subIndex + 1).Trim();
                        i = subIndex;
                        subIndex = -1;
                    }
                }
            }

            subIndex = -1;
            for (int i = 0; i < tempKeyword.Length; i++)
            {
                if ((tempKeyword[i].Equals(' ') && !tempKeyword[i + 1].Equals(' ')) || i == tempKeyword.Length - 1)
                {
                    stringKeyword.Add(tempKeyword.Substring(subIndex + 1, i - subIndex).Trim().ToUnsign().ToLower());
                    subIndex = i;
                }
            }
            try
            {
                List<Place> place;
                try
                {
                    //Tested with 1000 record
                    //Get from database: 765 miliseconds
                    //Get from XML data: 61 miliseconds
                    place = GetXMLData();
                    if (place.Count == 0)
                    {

                        place = db.Places.ToList();
                    }
                }
                catch (Exception e)
                {

                    place = db.Places.ToList();
                }
                List<Place> rslt;
                foreach (var s in stringKeyword)
                {
                    rslt = place.Where(p => (p.Name.ToUnsign().ToLower().SearchByWord(s) || p.Address.ToUnsign().ToLower().SearchByWord(s))
                        && (search.DistrictId <= 0 || p.DistrictId == search.DistrictId)
                        && (search.SubCategoryId <= 0 || p.SubCategoryId == search.SubCategoryId)).OrderBy(p => p.Id).ToList();
                    if (rslt != null && rslt.Count() > 0)
                    {
                        bool isExisted;
                        foreach (var p in rslt)
                        {
                            isExisted = false;
                            if (search.Result != null && search.Result.Count > 0)
                            {
                                foreach (var item in search.Result)
                                {
                                    if (p.Id == item.Place.Id)
                                    {
                                        item.Count++;
                                        isExisted = true;
                                        break;
                                    }
                                }
                                if (!isExisted)
                                {
                                    search.Result.Add(new HNHB.Models.PlaceModels.PlaceResult { Place = p, Count = 1 });
                                }
                            }
                            else
                            {
                                search.Result = new List<HNHB.Models.PlaceModels.PlaceResult>();
                                search.Result.Add(new HNHB.Models.PlaceModels.PlaceResult { Place = p, Count = 1 });
                            }
                        }
                    }
                }
                if (search.Result != null || search.Result.Count > 0)
                {
                    search.Result = search.Result.OrderByDescending(ps => ps.Count).ToList();
                }
            }
            catch (Exception e)
            {
                search.Result = null;
            }
            return search;
        }
예제 #2
0
 public ActionResult Search(HNHB.Models.PlaceModels.SearchModel model)
 {
     ViewBag.SubCategoryId = new SelectList(db.SubCategories, "Id", "Name");
     ViewBag.DistrictId = new SelectList(db.Districts, "Id", "DistrictName");
     var place = new PlaceModels();
     return View(place.Search(model));
 }