开发者

What is the best way to evaluate all occurrences of a field within a list in VB.NET?

开发者 https://www.devze.com 2023-02-22 18:11 出处:网络
I am new to VB.NET and I keep telling myself there must be a better way to do what I need to do several times a day which is look into a list and see if that one field is set to true anywhere in that

I am new to VB.NET and I keep telling myself there must be a better way to do what I need to do several times a day which is look into a list and see if that one field is set to true anywhere in that list.

So far, I've been using the For Each statement:

For Each player In ListOfPlayers  
  If player.isActive Then  
    ... do something ...  
  End If
Next

But I know some of the other languages can do something like:

if ListOfPlayers(*).isActive == true
开发者_如何学Go

which just seems more concise and to the point. Any suggestion?


As proposed by Bobby in the comments, use LINQ:

If ListOfPlayers.Any(Function (p) p.IsActive) Then …

This asks whether there’s any player (Any) for which IsActive is True.

0

精彩评论

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