Asp.net 如何检查arraylist是否已包含对象

Asp.net 如何检查arraylist是否已包含对象,asp.net,vb.net,webforms,Asp.net,Vb.net,Webforms,我正在尝试让我的购物车识别是否有商品已经添加到购物车中,然后将数量更新为一个,但它只是不断地将声明设置为false 这是我的页面加载处理程序,它根据查询字符串中发送的详细信息创建新产品 Private Sub ShoppingCartPage_Load(sender As Object, e As EventArgs) Handles Me.Load If Not (IsPostBack) Then If Not (Request.QueryString.ToString

我正在尝试让我的购物车识别是否有商品已经添加到购物车中,然后将数量更新为一个,但它只是不断地将声明设置为false

这是我的页面加载处理程序,它根据查询字符串中发送的详细信息创建新产品

Private Sub ShoppingCartPage_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not (IsPostBack) Then
        If Not (Request.QueryString.ToString().Length.Equals(0)) Then
            Dim newProduct As Product = New Product(Request.QueryString("ProductCode"), Request.QueryString("ProductName"), Request.QueryString("Category"), Val(Request.QueryString("Price")), Request.QueryString("Description"))

            If Session("shoppingCartSession") Is Nothing Then
                shoppingCart = New ArrayList()
                shoppingCart.Add(newProduct)
                Session("shoppingCartSession") = shoppingCart
            ElseIf (shoppingCart.Contains(newProduct.itemID)) Then
                For Each item As Product In shoppingCart
                    If item.Equals(Request.QueryString("ProductCode")) Then
                        item.updateQuantity()
                    End If
                Next

            Else
                shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
                shoppingCart.Add(newProduct)
                Session.Add("shoppingCartSession", shoppingCart)
            End If
        End If
        shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
        createShoppingCartTable()
    End If

End Sub

这里有很多问题。首先,如果要像这样添加到ArrayList:

shoppingCart.Add(newProduct)
然后添加产品对象。在这种情况下,您为什么认为这是有用的:

If (shoppingCart.Contains(newProduct.itemID)) Then
newProduct.itemID可能是一个整数或类似的值,因此集合中当然不包含它。它不包含任何整数值,因为它包含产品对象。您需要检查它是否包含具有该itemID的产品对象,而不是它是否直接包含该itemID。我将替换所有这些:

If Session("shoppingCartSession") Is Nothing Then
    shoppingCart = New ArrayList()
    shoppingCart.Add(newProduct)
    Session("shoppingCartSession") = shoppingCart
ElseIf (shoppingCart.Contains(newProduct.itemID)) Then
    For Each item As Product In shoppingCart
        If item.Equals(Request.QueryString("ProductCode")) Then
            item.updateQuantity()
        End If
    Next

Else
    shoppingCart = CType(Session("shoppingCartSession"), ArrayList)
    shoppingCart.Add(newProduct)
    Session.Add("shoppingCartSession", shoppingCart)
End If
为此:

shoppingCart = TryCast(Session("shoppingCartSession"), ArrayList)

If shoppingCart Is Nothing Then
    shoppingCart = New ArrayList
    Session("shoppingCartSession") = shoppingCart
End If

Dim existingProduct = shoppingCart.Cast(Of Product)().
                                   SingleOrDefault(Function(p) p.itemID = newProduct.itemID)

If existingProduct Is Nothing Then
    shoppingCart.Add(newProduct)
Else
    existingProduct.updateQualtity()
End If
也就是说,我也将遵循@VisualIncent的建议,使用产品列表而不是ArrayList。如果这样做,则可以在我建议的代码中省略Cast调用。

不要使用ArrayList。现在基本上已经过时了。改为使用强类型,或者如果要通过项ID提供查找,请使用。