locked
Convert C# to VB (LINQ - RemoveAll) RRS feed

  • Question

  • User-2089506584 posted

    Hi,

    I have this book from sanders (Pro MVC 1) and the code below is what I'm trying to convert from C# to VB.

    public void RemoveLine(Product product)
    {
    lines.RemoveAll(l => l.Product.ProductID == product.ProductID);
    }

    However, I don't have the intellisense for the RemoveAll method. [:(]

            Public Sub RemoveLine(ByVal product As ProductModel)
                Lines.RemoveAll(Function(c) c.Product.ProductID = product.ProductId)

            End Sub

    Sunday, September 13, 2009 11:16 AM

Answers

  • User681036950 posted

    I just tried this in VB.NET and C# and RemoveAll() is not available for the IList.  You have to use a System.Collections.Generic.List object to use RemoveAll().


    This will work - 

            Dim Lines As System.Collections.Generic.List(Of String)
            Lines.RemoveAll()

    Private _Lines As New List(Of CartLine)    

    Public Property Lines() As List(Of CartLine)

            Get

                Return _Lines

            End Get

            Set(ByVal value As List(Of CartLine))

                _Lines = value

            End Set

        End Property


            Public Sub RemoveLine(ByVal product As ProductModel)
                Lines.RemoveAll(Function(c) c.Product.ProductID = product.ProductId)

            End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 13, 2009 11:46 PM

All replies

  • User681036950 posted

    What type of object is Lines?

    Sunday, September 13, 2009 3:14 PM
  • User-2089506584 posted

    Hi hoopslife,

    It's type of IList

    Private _Lines As New List(Of CartLine)
            Public ReadOnly Property Lines() As IList(Of CartLine)
                Get
                    Return _Lines.AsReadOnly
                End Get
            End Property

    Public Class CartLine

            Private _Product As Product
            Public Property Product() As Product
                Get
                    Return _Product
                End Get
                Set(ByVal value As Product)
                    _Product = value
                End Set
            End Property

        End Class

    Sunday, September 13, 2009 8:55 PM
  • User681036950 posted

    I believe it's because you made the Lines variable readonly.  Try to remove that and see if intellisense works.

    Sunday, September 13, 2009 10:33 PM
  • User-2089506584 posted

    Try to remove that and see if intellisense works.

    Unfortunately, it did not. [:(]

    Maybe the VB version is indeed the Remove method. Is it?


    Sunday, September 13, 2009 11:06 PM
  • User681036950 posted

    I just tried this in VB.NET and C# and RemoveAll() is not available for the IList.  You have to use a System.Collections.Generic.List object to use RemoveAll().


    This will work - 

            Dim Lines As System.Collections.Generic.List(Of String)
            Lines.RemoveAll()

    Private _Lines As New List(Of CartLine)    

    Public Property Lines() As List(Of CartLine)

            Get

                Return _Lines

            End Get

            Set(ByVal value As List(Of CartLine))

                _Lines = value

            End Set

        End Property


            Public Sub RemoveLine(ByVal product As ProductModel)
                Lines.RemoveAll(Function(c) c.Product.ProductID = product.ProductId)

            End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 13, 2009 11:46 PM
  • User-1542018982 posted

    Hi,

    If the type is IList, I think RemoveAll method not exists.
    Refer link: http://msdn.microsoft.com/en-us/library/system.collections.ilist_members.aspx

    I think there is line class that Implements IList with RemoveAll method.

    Maybe links below can give you some ideas:
    http://www.eggheadcafe.com/software/aspnet/32543773/use-linq-to-remove-object.aspx
    http://www.aspfree.com/c/a/.NET/Generics-and-Interface-Based-Programming/

    I'm new to System.Collections namespace. Hope the info can help.

    Monday, September 14, 2009 12:01 AM
  • User-2089506584 posted

    Thank you so much, just like what I need! [:)]

    Monday, September 14, 2009 12:59 AM