Home > OS >  How to use a lambda expression to find a string in a list
How to use a lambda expression to find a string in a list

Time:09-16

I am trying to use a lambda expression to find a string within a list of items in a list. I am getting a instance not set to an object error.

Refer to this simple class as an example

Public Class Project
Public ProjectList as New List(Of ProjectType)
End Class

Public Class ProjectType
Public Name As String
End Class

In My Code:

Dim MyProjectCollection as New List(of Project)
Dim MyProjectType as New ProjectType
MyProjectType.Name = "Math" 
MyProjectCollection.ProjectList.AddItem(MyProjectType) 'adding items to the Project

i am trying to filter out only "Science" Projects like this

MyProjectCollection.RemoveAll(function(x as Project) x.ProjectType.Name <> "Science")

it errors, if anyone can point me to what I am doing wrong I would greatly appreciate it. I know I didn't add a ProjectType.Name = "Science" but just assume its a list already filled.

I want to remove all Projects where none of the ProjectTypes have a Name of "Science"

CodePudding user response:

You can use Exists in a second Lambda:

MyProjectCollection.RemoveAll(Function(p as Project) Not p.ProjectList.Exists(Function(pt As ProjectType) pt.Name = "Science"))

The first Lambda iterates over all Project objects in your list. The second Lambda checks if the Project's ProjectList contains a ProjectType with Science as its Name. This second Lambda will return True if Science exists. You can use Not to reverse that result.

CodePudding user response:

I think you want your classes to Properties not Fields.

Declare 2 ProjectType variables and set their Name properties.

Declare a Project variable.

Add the 2 ProjectType variables to the Project.ProjectList

Now do the RemoveAll method.

Public Class Project
    Public Property ProjectList As New List(Of ProjectType)
End Class

Public Class ProjectType
    Public Property Name As String
End Class

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim MyProjectType As New ProjectType
    MyProjectType.Name = "Math"
    Dim MyProjectType2 As New ProjectType
    MyProjectType2.Name = "Science"
    Dim Prj As New Project
    Prj.ProjectList.Add(MyProjectType) 'adding items to the Project's ProjectList
    Prj.ProjectList.Add(MyProjectType2)
    Debug.Print("List before remove")
    For Each pt As ProjectType In Prj.ProjectList
        Debug.Print(pt.Name)
    Next
    Prj.ProjectList.RemoveAll(Function(x As ProjectType) x.Name <> "Science")
    Debug.Print("List after remove")
    For Each pt As ProjectType In Prj.ProjectList
        Debug.Print(pt.Name)
    Next
End Sub

I have added a test loop before and after the remove. The results in the immediate window look like this.

List before remove 
Math 
Science 
List after remove 
Science

CodePudding user response:

I would use this:

MyProjectCollection.RemoveAll(Function(p) p.ProjectList.All(Function(pt) pt.Name <> "Science")))
  • Related