- Home /
Count selected item in List
Hello, I want to simply check number of selected item, in my case, of empty Item in inventory. I created enums for items and basic constructor to create them without parameters which set type to "NULL". This how I initialize ALL inventory slots. I am using this also when player remove item from inventory, instead of deleting it from List I am simply rewriting it to "new Item()", thats why simple .Count() won't work for me.
For now I have simple method:
int emptySlot = 0;
foreach(Item item in inventoryItems)
if(item.Type == Item.ItemType.NULL)
emptySlot++;
if(emptySlot == 0)
return;
But I am wonder if there is way to do something like:
if(inventoryItems.Count().Where(Item.ItemType.NULL) == 0)
Just to have less code =)
Answer by RafiXWPT · Jul 26, 2015 at 06:31 PM
Awwww! I tried with Linq and there is overload for .Count(), Now my method looks:
if(inventoryItems.Count(x => x.Type == Item.ItemType.NULL) == 0)
Sorry for stupid question, at least I figured it by myself!
Your answer
Follow this Question
Related Questions
Generic List.Count always gives 0 2 Answers
how add values to Generic.list ? 2 Answers
A node in a childnode? 1 Answer
Counting active objects in a list 3 Answers