C
C#2y ago
Ronnie

❔ Basic question on linear search in list

Hello, I am making a blog/notebook for an assignment and I have a list which contains vectors with 3 elements, title/date/body - i need to do a linear search for a title but I cant find any information on how to loop through the first element on all the vectors in the list, any ideas? Also, i have a DateTime-variable that is declared in the switch function, do i need to declare this at the top before using in Case 1?` Help much appreciated : - P
6 Replies
Angius
Angius2y ago
foreach (var book in books)
{
var title = book[0];
}
foreach (var book in books)
{
var title = book[0];
}
Should be as simple as that Or, if LINQ is permitted, you can do
foreach (var title in books.Select(book => book[0]))
{
// ...
}
foreach (var title in books.Select(book => book[0]))
{
// ...
}
Though seeing how you use an array instead of a class to represent a book, whatever course you're taking didn't even touch on classes yet So LINQ being permitted or talked about is unlikely
Ronnie
RonnieOP2y ago
We have touched on classes but i was specifically told not to use it for this assignment and to include a basic linear search for a title : - ) hmm soo.. i dont really understand, can you advice me what is wrong here on why this doesnt work?
Ronnie
RonnieOP2y ago
Angius
Angius2y ago
blogPostList[i] is an array You can't compare an array to a string The array of a book, far as I can tell, has the title on the 0th index So you should do searchWord == blogPostList[i][0]
Ronnie
RonnieOP2y ago
aaaaaaaaah! so [i][0] vill check index 0 of all objects in array? that was the missing piece, thank you! 😄
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