I am trying to find a way to look up a single word or multiple words from a list. The user inputs the word(s), and information about that item is fetched from the ItemList, based on the name.
For Example:
PriceList[0].name="Black Sheep"
PriceList[1].name="Black Horse"
PriceList[2].name="White Horse"
PriceList[3].name="White Sheep"
are some items in the list, where PriceList is an ItemList, that looks like:
public class ItemList
{
public int amount { get; set; }
public string name { get; set; }
public int buyprice { get; set; }
public int sellprice { get; set; }
public int stock { get; set; }
}
This is what I want my code to do:
- Case 1: User asks for "Black" : Return indices 0, 1.
- Case 2: User asks for "White" : Return 2, 3.
- Case 3: User asks for "Horse" : Return 1, 2.
- Case 4: User asks for "Sheep" : Return 0, 3.
- Case 5: User asks for "Black Horse" : Return 1.
- Case 6: User asks for "White Horse" : Return 2.
- Case 7: User asks for "Whit Horse" : Return 2 but not 1.
- Case 8: User asks for "Red Horse" : Return 1, 2.
etc.
I currently have:
int nickindex = PriceList.FindIndex(x => x.name.Split().Contains(typeToAdd));
where typeToAdd is the user input string.
However, this only returns one index, and it fails for cases 5 and up.
How can I loop over all indices to find them? I also need to be able to search for phrases rather than words. Lastly, I need to search within the words if no match was found (Case 7)
I have looked at Algorithm to find keywords and keyphrases in a string but it doesn't help me much.
Any help would be appreciated. Thank you.