❔ I'm trying to implement a search function using LINQ

https://paste.mod.gg/gctsykkoclwi/3

public void SearchBook() // Defining a method and use it to access the property list and search for books
        {
            Console.WriteLine("Choose from meny: "); // Dispaly a meny for a book type
            Console.WriteLine("\n\n\t [1] Search title"
                              + "\n\t [2] Search author");
            if (int.TryParse(Console.ReadLine(), out int meny))
            switch (meny)
            {
                case 1:
                    Console.WriteLine("Search by title: ");
                    var bookTitle = Console.ReadLine();
                    var searchTitle = bookShelf.Where(x => x.Title.Contains(bookTitle));

                        if (bookTitle != null)
                        {
                            foreach (var book in searchTitle)
                            {
                                Console.WriteLine(book);
                            }
                        }
                        break;
                case 2:
                    Console.WriteLine("Search by author: ");
                    var bookAuthor = Console.ReadLine();
                        var searchAuthor = bookShelf.Where(x => x.Author.Contains(bookAuthor));

                        if (bookAuthor != null)
                        {
                            foreach (var book in searchAuthor)
                            {
                                Console.WriteLine(book);
                            }
                        }
                        break;
            }
        }
A tool for sharing your source code with the world!
Was this page helpful?