public ActionResult Index(string searchKey = "") { ViewBag.searchKey = searchKey; List <Search_Result> Sr = new List <Search_Result>(); // Searching from PRoduct table var model = new List <Product>(); if (searchKey == "") { model = _context.Product.ToList(); } else { model = _context.Product.Where(x => x.ProductName.Contains(searchKey)).ToList(); } foreach (var item in model) { Search_Result sr = new Search_Result(); sr.Price = item.Price; sr.ProductId = item.ProductId; sr.ProductImage = item.ProductImage; sr.ProductName = item.ProductName; sr.Description = item.Description; sr.CategoryName = _context.Category.FirstOrDefault(x => x.CategoryId == item.CategoryId).CategoryName; Sr.Add(sr); } //Now searching from category table int count = _context.Category.Count(x => x.CategoryName.Contains(searchKey)); if (count > 0) { int Catid = _context.Category.FirstOrDefault(x => x.CategoryName.Contains(searchKey)).CategoryId; model = _context.Product.Where(x => x.CategoryId == Catid).ToList(); foreach (var item in model) { Search_Result sr = new Search_Result(); sr.Price = item.Price; sr.ProductId = item.ProductId; sr.ProductImage = item.ProductImage; sr.ProductName = item.ProductName; sr.Description = item.Description; sr.CategoryName = _context.Category.FirstOrDefault(x => x.CategoryId == item.CategoryId).CategoryName; Sr.Add(sr); } } _context.Dispose(); //List<Search_Result> sr = _unitOfWork.GetRepositoryInstance<Search_Result>().GetResultBySqlProcedure("USP_Search @searchKey", new SqlParameter("searchKey", SqlDbType.VarChar) { Value = searchKey }).ToList(); return(View(Sr)); }
// // Summary: // Creates a new instance of the class 'SearchResult' from EF // // Parameters: // searchResult: // Contains user 'SearchResult' // // Returns: // Returns void // public void UpdateDbResults(SearchResultModel searchResult) { try { using (FileExplorerEntities ef = new FileExplorerEntities()) { Search_Result newSearchResults = new Search_Result { SearchDetailsID = searchResult.SearchID, FullPath = searchResult.FullPath }; ef.Search_Results.Add(newSearchResults); ef.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }