Hello I am Trying to Filter Database Data base on ID but i cant get it to work;
Code:
- public async Task<ActionResult> Index(string searchString, string description)
- {
- var AllProducts = from m in _context.Products
- select m;
-
- List<int> IDList = new List<int>();
- IDList.Clear();
-
- int count = 0;
-
- filter base on Title
-
- if (!String.IsNullOrEmpty(searchString))
- {
- count++;
- var FilterSearch = from Products in AllProducts
- where Products.Title.Contains(searchString)
- select Products.Id;
-
- foreach (int Product in FilterSearch)
- IDList.Add(Product);
- }
-
- filter based on description
-
- if (!String.IsNullOrEmpty(description))
- {
- count++;
- var FilterDesc = from Products in AllProducts
- where Products.ProductDescription.Contains(description)
- select Products.Id;
-
- foreach (int Products in FilterDesc)
- IDList.Add(Products);
- }
-
- making sure that only data that have ID simulair appear
-
- if (IDList.Count != 0)
- {
- var filter2 = IDList.GroupBy(x => x)
- .Where(g => g.Count() == count)
- .Select(y => y.Key)
- .ToList();
-
-
- AllProducts = AllProducts.Where(x => x.Id.Equals(filter2));
-
- }
- return View(await AllProducts.ToListAsync());
-
- }
everything works except for this code on line 47:
- AllProducts = AllProducts.Where(x => x.Id.Equals(filter2));
when I type something in both search fields (description and searchStrings)
It return back nothing
Only when both search fields are empty they show all the data
I just need the Data Database that have simulair ID's as those of the numbers in filter2 list
How do i manage that?