VB.NET How to check list against another list RRS feed

  • Question

  • User941905567 posted

    I am basically trying to check and see if an item is contained in a list . (See the .Contains() below) The list is a list of a class model in my project. What would I need to do to get this to work ? The error I get currently is "Value of type 'String' cannot be converted to 'AgentModAvailability' " ModId is a string value. Any suggestions would be greatly appreciated.  


    'Load the vms object 
    Dim vms As List(Of Mods2Vm) = Mods2GetVms()
    'Load agtspecificmods
      Dim bllagtspecificmods As Premium.BLL.AgentModAvailability = New Premium.BLL.AgentModAvailability()
      Dim agtspecificmods As New List(Of Premium.Model.AgentModAvailability)
      agtspecificmods = bllagtspecificmods.LoadArr().ToList()
     For Each v As Mods2Vm In vms
         If agtspecificmods.Contains(v.ModId) = True Then
            'Do Stuff
         END IF

    Wednesday, March 11, 2020 1:24 PM

All replies

  • User281315223 posted

    You could evaluate all of the items within your collection to see if any meet your specific criteria (e.g. they have that specific ID) via LINQ:

    agtspecificmods.Any(Function(v) v.SomeProperty = v.ModId) = True

    Wednesday, March 11, 2020 8:59 PM
  • 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"
            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"
            For Each v As Mods2Vm In vms
                If agtspecificmods.Exists(Function(x) x.property1 = v.ModId) Then
                End If
        End Sub
        Public Class Mods2Vm
            Public Property ModId As String
        End Class
        Public Class AgentModAvailability
            Public Property property1 As String
        End Class


    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

    Thursday, March 12, 2020 3:16 AM