开发者

how to select instance from a list in c#

开发者 https://www.devze.com 2023-01-20 12:14 出处:网络
i have a struct who have a list of item and some other variable about struct i want to check t开发者_如何学编程hat a enum in list have a specific value or not.

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

i want to check t开发者_如何学编程hat 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


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?


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消