Find an item in the (Of T) list with x, y, and z

I have the following setup:

Class A
      property x as string
      property y as int
      property z as String

End Class

Class CollOfA
    inherits List(Of A)

End Class

What I need is the Item property in the collection, which I can say:

dim c as new CollOfA
c.item("this", 2, "that")

I tried to implement the following in CollOfA:

Class CollOfA
    inherits List(Of A)

    Default Public Overridable Shadows ReadOnly Property Item(ByVal x As String, ByVal y As Integer, byval z as string)
        Get
            ' I want to do something like:
            ' ForEach item in me see if anything matches these three things

        End Get
    End Property
End Class

I know about predicates, but I'm struggling with how to set predicate criteria (i.e. pass x, y, and z).

Has anyone implemented something similar?

+3
source share
2 answers

Here are two ways I came across to resolve your issue. One way uses LINQ query syntax to filter; the second uses a custom object to store your predicate parameters, and then uses this object to execute the filter.

Using LINQ syntax in the Item property:

Default Public Overridable Shadows ReadOnly Property Item(ByVal x As String, ByVal y As Integer, ByVal z As String) As IEnumerable(Of A)
    Get
        Return (From theA In Me
                Where (theA.x = x And theA.y = y And theA.z = z)
                Select theA)

    End Get
End Property

- PredicateParameter , , . MSDN - . :

Class PredicateParams

    Public Sub New(ByVal theA As A)
        Criteria = theA
    End Sub

    Public Property Criteria As A

    Public Function IsMatch(ByVal theA As A) As Boolean
        Return (theA.x = Criteria.x And theA.y = Criteria.y And theA.z = Criteria.z)
    End Function

End Class

, CollOfA, :

Public Overridable Shadows ReadOnly Property ItemPred(ByVal x As String, ByVal y As Integer, ByVal z As String) As IEnumerable(Of A)
    Get
        Dim predA As New A
        predA.x = x
        predA.y = y
        predA.z = z

        Dim pred As New PredicateParams(predA)

        Return Me.FindAll(AddressOf pred.IsMatch)
    End Get

End Property

, , .

Sub Main()
    Dim mycoll As New CollOfA()


    For index = 1 To 100
        Dim anA As New A()
        anA.x = (index Mod 2).ToString()
        anA.y = index Mod 4
        anA.z = (index Mod 3).ToString()
        mycoll.Add(anA)
    Next

    Dim matched As IEnumerable(Of A) = mycoll.Item("1", 3, "2")
    Dim matched2 As IEnumerable(Of A) = mycoll.ItemPred("1", 3, "2")

    Console.WriteLine(matched.Count.ToString())  'output from first search
    Console.WriteLine(matched2.Count.ToString()) 'output from second search (s/b same)
    Console.ReadLine()

End Sub

, . , . (, #, VB.NET .)

+6

A , , - :

Public Class CollOfA
    Inherits List(Of A)

    Default Public Overloads ReadOnly Property Item(ByVal x As String, ByVal y As Integer, ByVal z As String) As A
        Get
            Return Find(Function(a As A) (((a.x = x) AndAlso (a.y = y)) AndAlso (a.z = z)))
        End Get
    End Property
End Class

Visual Studio 2008, , .

+1

Source: https://habr.com/ru/post/1756593/


All Articles