❔ 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
Pobiega14mo ago
What is the question? also, bookShelf.Where(...) will never return null, so the nullcheck isn't needed
ero
ero14mo ago
it's not checking the returned ienumerable for null
Pobiega
Pobiega14mo ago
the returned ienumerable will never be null .Where cant return null
ero
ero14mo ago
it is not checking that!
Pobiega
Pobiega14mo 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
Pobiega14mo 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
Pobiega14mo ago
yes
The king of kings
I find it a bit hard to understand these extension functions like Where() firstOrDefault() Contains()
Pobiega
Pobiega14mo 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
Pobiega14mo 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
friedice14mo 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
Jimmacle14mo 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
friedice14mo 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
friedice14mo 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
friedice14mo 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
friedice14mo ago
unless you're doing maths no point to use them as raw numbers makes everything easier
The king of kings
Ok
Jimmacle
Jimmacle14mo 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
Jimmacle14mo 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
Jimmacle14mo 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
Jimmacle14mo 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
Accord14mo 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
More Posts
❔ Blazor external loginHi folks, I'm working with a Blazor Server project, where I am using Identity for inbuilt account r❔ ASP.NET Core, OrchardCore, OrchardCMSI am starting to lose it, I've been following the tutorials of the OrchardCore documentation to add ❔ ✅ ASP.NET Core - Implementing an API in CQRS patternHey everyone, I am trying to make API integration to my ASP.NET Core app. I did something but I am n❔ Unable to process post request with asp.net coreI am making use of a document processing component (https://ej2.syncfusion.com/react/documentation/d❔ Socket connection in WPF applicationHello, I'm fairly new to the C# environment and there's something I can't figure out: I created a co❔ Force log out the user after account deletion with JWT bearer auth.Hello, I'm working on adding a feature that allows administrators to delete users. As things current✅ I updated to .NET 8 and now it broke...Hello i thought about .NET 8 and it has very usefull features so i want to update to it but sadly wh✅ Compiling ASP.NET 7 app using Github ActionsI am trying to compile my ASP.NET 7 application solution using Github actions but am getting the fol❔ How to handle a group of variables that need to stay together within a class?How should I handle a group of data that needs to stay together within a class? The data is a set of❔ .NET MAUI MVVM Navigation and pass Object to new page, Throw Exception: Missing methodProgram crash after this code ```[RelayCommand] async Task GoApartmentDetail(Apartment apart