How can I speed up the execution of this search-related code in EF asp mvc?

Viewed 9

I used this code to search among the site's products and all the fields in the database have indexes but the loading speed is very low.

The higher the number of results found, the slower the speed and the user has to wait for about 15 seconds

This part of the code finds data that is searched with words and in another function the filters are clearly applied

public List<Product> AutoCompleteProductList(string term = "")
    {
        string[] words = term.Split(' ');
        List<SearchResult> v = new List<SearchResult>();
        List<int> temp = new List<int>();
        if (!String.IsNullOrEmpty(term))
        {
            foreach (var text in words)
            {
                temp.Clear();
                temp = db.Products.Where(p => p.Product_Name.Contains(text)
                || p.Product_English_Name.Contains(text)
                || p.Product_ID.ToString() == text
                || p.Product_Short_Info.Contains(text)
                || p.Product_Tag.Contains(text)).Where(a => a.Product_Status).OrderByDescending(a => a.Product_ID).Select(r => r.Product_ID).ToList();
                foreach (var item in temp)
                {
                    SearchResult SearchResultTemp = v.FirstOrDefault(e => e.Product_ID == item);
                    if (SearchResultTemp != null)
                    {
                        SearchResultTemp.Count += 1;
                        v.RemoveAt(v.IndexOf(SearchResultTemp));
                        v.Add(SearchResultTemp);
                    }
                    else
                    {
                        v.Add(new SearchResult
                        {
                            Product_ID = item,
                            Count = 1
                        });
                    }
                }
            }
        }

        List<int> end = v.OrderByDescending(e => e.Count).Select(e => e.Product_ID).ToList();
        List<Product> routeList = new List<Product>();
        foreach (var item in end)
        {
            Product p = db.Products.Find(item);
            routeList.Add(p);
        }
        return routeList;
    }
0 Answers
Related