简体   繁体   中英

Check if a List already contains an item or not?

I used a struct

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

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

How i can check the list already have a stuff "where ID = 1"?

您可以非常轻松地使用LINQ

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM