DELETE me
DELETE me

Reputation:

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"?

Upvotes: 6

Views: 6461

Answers (3)

Dennis C
Dennis C

Reputation: 24747

You can use LINQ very easily

bool res = stuff.Any(c => c.ID == 1);

Upvotes: 17

Coding Flow
Coding Flow

Reputation: 21881

if(stuf.Select(x => x.id).Contains(1))
{
    //Do Stuff
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

bool isContains = stuff.Any(x => x.ID == 1);

Upvotes: 8

Related Questions