Quick check of all sub items in a List
I have a list with a boolean value as one of the properties. I need to check if every one开发者_运维知识库 of these is true. Now normally LINQ would give a nice clean answer here, but as I am using .NET 2.0 I can't use that.
If there a nicer way to do this or am I going to have to do a for each loop and break out on finding a false?
Edit:
Seems I could have been a bit clearer. I have an object in a list (eg List (Of MyObject)
). There is a boolean property on this object called Processed.
I need to check that all objects in the list are processed.
So in LINQ I'd do:
if (from o in list where o.processed = false select o).count = 0 then....
By using List<Of Boolean>
you can use Contains()
method:
If MyList.Contains(false) Then
// at least one false
Else
// all true
End If
ArrayList
and List<T>
has a bunch of methods that does pretty much what LINQ does. They have been there before LINQ.
Specifically, look at the Exists
method. Unfortunately I dont know the VB syntax :)
You could write your own generic list implementation an add a property IsProcessedCompletely:
Public Class My_Class
Public Property IsProcessed() As Boolean
Get
End Get
Set(ByVal value As Boolean)
End Set
End Property
End Class
Public Class My_List
Inherits List(Of My_Class)
Public ReadOnly Property IsProcessedCompletely() As Boolean
Get
Dim enumerator As List(Of My_Class).Enumerator = MyBase.GetEnumerator
While enumerator.MoveNext
If Not enumerator.Current.IsProcessed Then
Return False
End If
End While
Return True
End Get
End Property
End Class
精彩评论