I have a public class.
public class StoreItems
{
public string itemName;
public string itemPrice;
public string itemQuantity;
}
I have a list.
public List <StoreItems> itemData = new List<StoreItems> ();
The user will enter an item name and im supposed to check if that item name is already in my itemData's itemName.
My current code is something like this
if (itemData.Find(x => x.itemData.Equals(userInput))
{
//already in list
}
else
{
//add data
}
However, I'm getting an error saying itemData cannot be implicitly converted to bool. Tips would be appreciated
You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
Contains(T) Method is used to check whether an element is in the List<T> or not.
Select(x => new { x, count = x. tags. Count(tag => list. Contains(tag)) }) .
The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise.
You can achieve this easily with LINQ.
if(itemData.Any(data => data.itemName == userInput))
Any
checks all items of an IEnumerable
whether they match a given predicate, or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With