User-719153870 posted
Hi Jazzcatone,
In addition to @Rion's reply, you can also use the Exists() method to check if a model with specific property value exists in the list.
Sub Main()
Dim agtspecificmods As List(Of AgentModAvailability) = New List(Of AgentModAvailability)()
Dim agent1 As AgentModAvailability = New AgentModAvailability() With {
.property1 = "a"
}
Dim agent2 As AgentModAvailability = New AgentModAvailability() With {
.property1 = "b"
}
Dim agent3 As AgentModAvailability = New AgentModAvailability() With {
.property1 = "c"
}
agtspecificmods.Add(agent1)
agtspecificmods.Add(agent2)
agtspecificmods.Add(agent3)
Dim vms As List(Of Mods2Vm) = New List(Of Mods2Vm)()
Dim mods21 As Mods2Vm = New Mods2Vm() With {
.ModId = "aa"
}
Dim mods22 As Mods2Vm = New Mods2Vm() With {
.ModId = "b"
}
Dim mods23 As Mods2Vm = New Mods2Vm() With {
.ModId = "cc"
}
vms.Add(mods21)
vms.Add(mods22)
vms.Add(mods23)
For Each v As Mods2Vm In vms
If agtspecificmods.Exists(Function(x) x.property1 = v.ModId) Then
Console.WriteLine("1")
Else
Console.WriteLine("0")
End If
Next
End Sub
Public Class Mods2Vm
Public Property ModId As String
End Class
Public Class AgentModAvailability
Public Property property1 As String
End Class
Output:

For more information, please check the doc List<T>.Contains(T) Method.
Why not use the Contains method, it's right there in the doc example?
I've tested it too and it works perfecetly in c# but not in vb.
To make Contains() feasible, it requires the class to inherit from itself:
public class Part : IEquatable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
}
This can work in c#, but in vb it throws error:

I'm not export, but it seems that "Classes can inherit only from other classes" in vb.
Best Regard,
Yang Shen