Steven Spielberg
Steven Spielberg

Reputation:

how to select instance from a list in c#

i have a struct who have a list of item and some other variable about struct

i want to check that a enum in list have a specific value or not.

like

struct.list.havevalue == 5;

how i can count all who have the specific value in enum in the itemlist of the structure

Upvotes: 0

Views: 478

Answers (3)

Ivan
Ivan

Reputation: 1

List.IndexOf(T) method will help you. link text

Note, that this method (as any of the suggested Linq solutions) is an O(n) operation. So if you are concerned about the performance of lookup routine you might consider to convert List<T> into HashSet<T> or any other hashtable-based collection depending on your requirements.

Upvotes: 0

danijels
danijels

Reputation: 5291

If you by "list" mean IList<int> or something similar, that would be like:

struct.list.Count(i => i == 5);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501163

Your question isn't really clear, but it sounds like you might want to use LINQ:

int count = x.list.Count(v => v.Value == 5);

But without anything more specific about what types are involved, it's very hard to say. If you could provide more details - such as the declaration of the types involved - it would really help.

By the way, it's very odd for a struct to contain a list. Are you really sure you want to be using a struct rather than a class?

Upvotes: 6

Related Questions