Check if List already contains an item or not?

I used struct

public struct stuff
{
    public int ID;
    public int quan;
}

in List<stuff> stuff = new List<stuff>();

How can I check the list, is there already a material "where ID = 1"?

+5
source share
3 answers

You can easily use LINQ

bool res = stuff.Any(c => c.ID == 1);
+13
source
bool isContains = stuff.Any(x => x.ID == 1);
+7
source
if(stuf.Select(x => x.id).Contains(1))
{
    //Do Stuff
}
+1
source

All Articles