❔ 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;
}
}
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;
}
}
BlazeBin - gctsykkoclwi
A tool for sharing your source code with the world!
39 Replies
Pobiega
Pobiega2y ago
What is the question? also, bookShelf.Where(...) will never return null, so the nullcheck isn't needed
ero
ero2y ago
it's not checking the returned ienumerable for null
Pobiega
Pobiega2y ago
the returned ienumerable will never be null .Where cant return null
ero
ero2y ago
it is not checking that!
Pobiega
Pobiega2y ago
you're right, what they wrote makes even less sense you are checking it for null AFTER using it
var bookTitle = Console.ReadLine();
var searchTitle = bookShelf.Where(x => x.Title.Contains(bookTitle)); // can be null here

if (bookTitle != null)
{
// cant be null here
}
var bookTitle = Console.ReadLine();
var searchTitle = bookShelf.Where(x => x.Title.Contains(bookTitle)); // can be null here

if (bookTitle != null)
{
// cant be null here
}
so you've already done the search before you check for null
The king of kings
Sorry! I wasn't able to search by title neither by author, because I keep miss adding the Console.ReadLine(); but now it's working.
Pobiega
Pobiega2y ago
ugh not a big fan of that
The king of kings
Oh! Really. Interesting to know. Ok! I should modify the code then.
Pobiega
Pobiega2y ago
yes
The king of kings
I find it a bit hard to understand these extension functions like Where() firstOrDefault() Contains()
Pobiega
Pobiega2y ago
how so? Where returns all results that matched the "filter" in an IEnumerable (0..n) FirstOrDefault returns the first result that matches, or null if no item matched (null or 1)
The king of kings
I mean the docs provides too much dame informations which leads to be confused what to use and what not to use.
Pobiega
Pobiega2y ago
there are multiple ways of calling it.
The king of kings
Wow, you just summarized these methods in a short and understandable way.
friedice
friedice2y ago
Is it a requirement to search by specific type? Why not search for both? Since searchtitle is an ienum of books, when you're writing the book to the console, isn't it an object, don't you want book.Title?
Jimmacle
Jimmacle2y ago
iirc the assignment made him override ToString instead
The king of kings
Though the output looks a bit awkward, I mean when the user search, the output should be something like The book Harry Potter is in the Novel section
The king of kings
Didn't quite get your point? Yes, I'm trying to search either by title or author.
friedice
friedice2y ago
why not just implement the linq to handle title or author in 1 search? is that part of the requirement
The king of kings
Nope, according to the requirements actually it's not required to search separately. But these are extra where they want me to implement for practicing sake
Enables your search to search by publication year, i.e. integer (int).
Work with access levels other than Public and Private.
Enables your program to delete all saved books.
Enables your search to search by publication year, i.e. integer (int).
Work with access levels other than Public and Private.
Enables your program to delete all saved books.
friedice
friedice2y ago
it would probably be easier to implement a search that searches for all of those criterias together as an OR instead of searching seperately then
The king of kings
Ok! You're right. I can do that too. Should I just delete the author function? I think it's useless to search by publishing year too, what d y think? They have mentioned in the above that the project should be .NET Framework the problem is I use MacBook so I will need to purchase a Windows device tomorrow and configure this code in the project.
friedice
friedice2y ago
i mean it shouldnt be too difficult to add the search for publishing year, and whatever else the book as for the prop just convert everything to string
The king of kings
Ok! You mean when the user inputs like 1999 I should convert it from int to string?
friedice
friedice2y ago
unless you're doing maths no point to use them as raw numbers makes everything easier
The king of kings
Ok
Jimmacle
Jimmacle2y ago
you can probably ignore the .NET framework requirement unless this code is expected to run on a specific device
The king of kings
Ok! I totally agree with you. Other wise I'm not even intended to build apps for only Windows platform. My goal is to build cross-platform apps using .NET Maui that work on Windows & iOS and I could use my work Mac to accomplish this goal, why would I spend hundreds bucks on a new device just to finish my assignments in this course 🤷🏻‍♂️
Jimmacle
Jimmacle2y ago
idk, why would you
The king of kings
Yeah! Although, the online course that I'm taking currently belongs to one of the official schools in my district, so it's not just a payed local course, so I might not be granted any certificate or any validation at the end if I don't meet the course's conditions or requirements 🤔 What do you think?
Jimmacle
Jimmacle2y ago
i think you need to ask your teacher
The king of kings
Yep, this is the best thing I can do. Thanks a lot for your advice 🤗 I got this message from my teacher
.Net exists for several generations, if you run the .net framework, it is 4.8 which is the newest, 7 works well for most things, but on mac there is no support for windows forms in any version because it is a component connected to windows and it is this is what the course, sample assignments and the book are based on. For programming 2, it is strongly recommended that you run Windows. Unfortunately, there is no support for all alternative solutions, and then you have to Google your way. Alternatively, install virtual box or dualboot on your mac so you can run windows.
.Net exists for several generations, if you run the .net framework, it is 4.8 which is the newest, 7 works well for most things, but on mac there is no support for windows forms in any version because it is a component connected to windows and it is this is what the course, sample assignments and the book are based on. For programming 2, it is strongly recommended that you run Windows. Unfortunately, there is no support for all alternative solutions, and then you have to Google your way. Alternatively, install virtual box or dualboot on your mac so you can run windows.
@Jimmaclewhat do you think from your experience?
Jimmacle
Jimmacle2y ago
i think you should be able to figure it out at this point and if your teacher says you will need winforms then you need to run windows
The king of kings
Ok! I solved the both issues so far. I had a wrong order in the sub-class parameters that's all Regarding the Windows forms, I solved the issue by the virtualbox.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server