❔ I'm trying to implement a search function using LINQ
BlazeBin - gctsykkoclwi
A tool for sharing your source code with the world!
39 Replies
What is the question?
also,
bookShelf.Where(...)
will never return null, so the nullcheck isn't neededit's not checking the returned ienumerable for null
the returned ienumerable will never be null
.Where
cant return nullit is not checking that!
you're right, what they wrote makes even less sense
you are checking it for null AFTER using it
so you've already done the search before you check for null
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.ugh
not a big fan of that
Oh! Really. Interesting to know.
Ok! I should modify the code then.
yes
I find it a bit hard to understand these extension functions like
Where() firstOrDefault() Contains()
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)I mean the docs provides too much dame informations which leads to be confused what to use and what not to use.
Enumerable.FirstOrDefault Method (System.Linq)
Returns the first element of a sequence, or a default value if no element is found.
there are multiple ways of calling it.
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.firstordefault?view=net-7.0#system-linq-enumerable-firstordefault-1(system-collections-generic-ienumerable((-0))-system-func((-0-system-boolean)))
this one is the one that takes a predicate
Enumerable.FirstOrDefault Method (System.Linq)
Returns the first element of a sequence, or a default value if no element is found.
Wow, you just summarized these methods in a short and understandable way.
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?
iirc the assignment made him override
ToString
insteadThough 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
Didn't quite get your point? Yes, I'm trying to search either by title or author.
why not just implement the linq to handle title or author in 1 search? is that part of the requirement
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
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
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.
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
Ok! You mean when the user inputs like 1999 I should convert it from int to string?
unless you're doing maths no point to use them as raw numbers
makes everything easier
Ok
you can probably ignore the .NET framework requirement unless this code is expected to run on a specific device
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 🤷🏻♂️
idk, why would you
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?
i think you need to ask your teacher
Yep, this is the best thing I can do. Thanks a lot for your advice 🤗
I got this message from my teacher
@Jimmaclewhat do you think from your experience?
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
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.
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.