ARTICLE AD BOX
There is no single correct answer here. If the code you have works, then there's nothing inherently wrong with it. The number of recipes matters, though. Looks like your code loads the entire recipe collection into memory, and then filters on the server. Consider using Entity framework queries, which will generate SQL statements to filter data on the database, rather than in-memory on the server.
For a toy project, this is fine. Real world applications would probably go with Entity Framework queries or stored procedures so the server only needs to deal with the results of a filtered query, rather than every record in a particular table.
Regarding handling null or empty search queries, what you have is fine. Think of search as adding additional filters. If you don't specify a recipe name, don't filter on the name. Missing search criteria aren't necessarily errors; they just indicate "don't filter results based on this criteria."
19.5k10 gold badges59 silver badges108 bronze badges
but I am unsure if using "StringComparison.OrdinalIgnoreCase" is the best practice for a web application
I don't know if it is "best" but it can be good as it allows users to input the seach words in small letters only.
if there is a more efficient way to handle null or empty search strings.
It depends on how you want to handle "null or empty search strings" from users. It seems to me that you want return unfiltered result if a user sends "null or empty search strings." If so your code is OK.
Please note that Console.WriteLine("this is search: " + search); does not make sense in the Web app.
Explore related questions
See similar questions with these tags.
