public IEnumerable <Book> SearchBooks(string query, string order, string direction) { List <string> parts = query.ToLower().Split('+', StringSplitOptions.RemoveEmptyEntries).ToList(); Dictionary <Book, int> Relevance; if (parts.Count > 0) { var partsWithDashes = parts.Where(x => x.Contains('-')); var noDashParts = partsWithDashes.Select(x => x.Replace("-", "")).ToList(); if (noDashParts.Count > 0) { parts.AddRange(noDashParts); } Relevance = new Dictionary <Book, int>(); foreach (var part in parts) { var bookWithKeyword = _context.Books .Where(p => $"{p.Name} {p.Edition} {p.Author} {p.ISBN10} {p.ISBN13}" .Contains(part, StringComparison.InvariantCultureIgnoreCase)); foreach (var book in bookWithKeyword) { if (Relevance.ContainsKey(book)) { Relevance[book]++; } else { Relevance.Add(book, 1); } } } } else { Relevance = _context.Books.ToDictionary(x => x, x => 1); } foreach (var book in Relevance) { if (book.Key.User == null) { book.Key.User = GetUser(book.Key.UserId, false); } } switch (order) { case "relevance": if (direction == "asc") { return(Relevance.OrderByDescending(x => x.Value).Select(x => x.Key)); } else if (direction == "desc") { return(Relevance.OrderBy(x => x.Value).Select(x => x.Key)); } break; case "price": if (direction == "asc") { return(Relevance.OrderBy(x => x.Key.Price).Select(x => x.Key)); } else if (direction == "desc") { return(Relevance.OrderByDescending(x => x.Key.Price).Select(x => x.Key)); } break; case "name": if (direction == "asc") { return(Relevance.OrderBy(x => x.Key.Name).Select(x => x.Key)); } else if (direction == "desc") { return(Relevance.OrderByDescending(x => x.Key.Name).Select(x => x.Key)); } break; } return(new List <Book>()); }