From bcdd04f42659be335cbad7cb2dd960aac7a5a7ee Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Tue, 18 Oct 2022 10:17:28 -0700 Subject: [PATCH] Update SearchController.cs --- .../Controllers/SearchController.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 1acf0a8..2d63a8f 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -20,17 +20,24 @@ public class SearchController : Controller { [HttpPost(Name = "Search")] public IEnumerable Post(string query, bool? preview) { const int PREVIEW = 3; - HashSet products = new(); + List products = new(); query = query.Trim(); try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) - products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToHashSet(); + products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); else { - products.Concat(_context.Products.Where(x => x.Title.Contains(query)).ToHashSet()); - products.Concat(_context.Products.Where(x => x.Category.Contains(query)).ToHashSet()); - products.Concat(_context.Products.Where(x => x.Description.Contains(query)).ToHashSet()); + products = _context.Products.Where(x => x.Title.Contains(query)).ToList(); + + foreach (Product prod in _context.Products.Where(x => x.Category.Contains(query)).ToList()) { + if (!products.Contains(prod)) + products.Add(prod); + } + foreach (Product prod in _context.Products.Where(x => x.Description.Contains(query)).ToList()) { + if (!products.Contains(prod)) + products.Add(prod); + } } } catch (Exception e) { _logger.LogError(8, e.Message);