如何对对象的属性进行排序并填充到 ArrayList 中
How to sort properties of object and populate into an ArrayList
如何对 class 中的以下属性进行排序,然后存储在 ArrayList
中?
我已将其填充到 ArrayList
但是当页面重新加载时出现问题,中继器中的项目顺序将更改。这是数组
中的人口 cookie 值
Dim myCookies As HttpCookie=HttpContext.Current.Request.Cookies("Mycard")
Dim varArryItems As ArrayList = New ArrayList
For i AsInteger=0 To varCookies.Values.Count-1
Dim AllValues As String()=myCookies.Values(i).Split("|"c)
Dim item As objCard=New objCard
item.P_ItemID=Integer.Parse(AllValues(0))
item.P_ItemTitle=AllValues(1).ToString
item.P_BrandTitle=AllValues(2).ToString
item.P_ItemImg=AllValues(3).ToString
item.P_ItemPrice=Decimal.Parse(AllValues(4))
'item.P_ItemQauntity=Integer.Parse(AllValues(5))
'item.P_ItemQauntitySelected=Integer.Parse(AllValues(6))
item.P_BarcodeID=Integer.Parse(AllValues(7))
item.P_TotalItemPrice=Decimal.Parse(AllValues(8))
varArryItems.Add(item)
Next
rptcart.DataSource=varArryItems
rptcart.DataBind()
这是我的 objCard
class 存储的 cookie 值我需要对我尝试起诉的所有属性进行排序 ArrayList
排序方法对我不起作用。
Public Class objCard
Private ID As Integer
Private ItemID As Integer
Private BarcodeID As Integer
Private ItemTitle As String
Private BrandTitle As String
Private ItemImg As String
Private ItemPrice As Decimal
Private TotalItemPrice As String
Private ItemQauntity As Integer
Private ItemQauntitySelected As Integer
Public Property P_ID As Integer
Get
Return Me.ID
End Get
Set
Me.ID = Value
End Set
End Property
Public Property P_ItemID As Integer
Get
Return Me.ItemID
End Get
Set
Me.ItemID = Value
End Set
End Property
Public Property P_BarcodeID As Integer
Get
Return Me.BarcodeID
End Get
Set
Me.BarcodeID = Value
End Set
End Property
Public Property P_ItemTitle As String
Get
Return Me.ItemTitle
End Get
Set
Me.ItemTitle = Value
End Set
End Property
Public Property P_BrandTitle As String
Get
Return Me.BrandTitle
End Get
Set
Me.BrandTitle = Value
End Set
End Property
Public Property P_ItemImg As String
Get
Return Me.ItemImg
End Get
Set
Me.ItemImg = Value
End Set
End Property
Public Property P_ItemPrice As Decimal
Get
Return Me.ItemPrice
End Get
Set
Me.ItemPrice = Value
End Set
End Property
Public Property P_TotalItemPrice As String
Get
Return Me.TotalItemPrice
End Get
Set
Me.TotalItemPrice = Value
End Set
End Property
Public Property P_ItemQauntity As Integer
Get
Return Me.ItemQauntity
End Get
Set
Me.ItemQauntity = Value
End Set
End Property
Public Property P_ItemQauntitySelected As Integer
Get
Return Me.ItemQauntitySelected
End Get
Set
Me.ItemQauntitySelected = Value
End Set
End Property
End Class
如果你有
Public Class Card
Property ID As Integer
Property ItemID As Integer
Property BarcodeID As Integer
Property ItemTitle As String
Property BrandTitle As String
Property ItemImg As String
Property ItemPrice As Decimal
Property TotalItemPrice As Decimal
Property ItemQuantity As Integer
Property ItemQuantitySelected As Integer
End Class
然后你可以使用List(Of Card)
来存储数据。这样做的好处是编译器知道它在其中实例化了 Card
而不仅仅是某个对象。
Dim myCookies As HttpCookie = HttpContext.Current.Request.Cookies("Mycard")
Dim cards = New List(Of Card)
For i As Integer = 0 To varCookies.Values.Count-1
Dim allValues As String() = myCookies.Values(i).Split("|"c)
Dim item = New Card
item.ItemID = Integer.Parse(allValues(0))
item.ItemTitle = allValues(1).ToString
item.BrandTitle = allValues(2).ToString
item.ItemImg = allValues(3).ToString
item.ItemPrice = Decimal.Parse(allValues(4))
'item.ItemQuantity = Integer.Parse(allValues(5))
'item.ItemQuantitySelected = Integer.Parse(allValues(6))
item.BarcodeID = Integer.Parse(allValues(7))
item.TotalItemPrice = Decimal.Parse(allValues(8))
cards.Add(item)
Next
现在编译器可以获取列表中条目的属性,您可以
Dim dataToPresent = cards.OrderBy(function(c) c.ItemId).ToList()
rptcart.DataSource = dataToPresent
rptcart.DataBind()
它会按照您选择的顺序显示数据。
如果您需要在 运行 时按不同的属性进行排序,那么搜索 "linq dynamic orderby" 应该会为您提供有用的代码。
我注意到您的 Private TotalItemPrice As String
与 item.P_TotalItemPrice=Decimal.Parse(AllValues(8))
冲突。如果你使用 Option Strict On
那么 Visual Studio 会为你指出类似的问题。
P.S。你有 Dim myCookies
但你使用 varCookies.Values.Count
。您可能想检查一下是否正确。
如何对 class 中的以下属性进行排序,然后存储在 ArrayList
中?
我已将其填充到 ArrayList
但是当页面重新加载时出现问题,中继器中的项目顺序将更改。这是数组
Dim myCookies As HttpCookie=HttpContext.Current.Request.Cookies("Mycard")
Dim varArryItems As ArrayList = New ArrayList
For i AsInteger=0 To varCookies.Values.Count-1
Dim AllValues As String()=myCookies.Values(i).Split("|"c)
Dim item As objCard=New objCard
item.P_ItemID=Integer.Parse(AllValues(0))
item.P_ItemTitle=AllValues(1).ToString
item.P_BrandTitle=AllValues(2).ToString
item.P_ItemImg=AllValues(3).ToString
item.P_ItemPrice=Decimal.Parse(AllValues(4))
'item.P_ItemQauntity=Integer.Parse(AllValues(5))
'item.P_ItemQauntitySelected=Integer.Parse(AllValues(6))
item.P_BarcodeID=Integer.Parse(AllValues(7))
item.P_TotalItemPrice=Decimal.Parse(AllValues(8))
varArryItems.Add(item)
Next
rptcart.DataSource=varArryItems
rptcart.DataBind()
这是我的 objCard
class 存储的 cookie 值我需要对我尝试起诉的所有属性进行排序 ArrayList
排序方法对我不起作用。
Public Class objCard
Private ID As Integer
Private ItemID As Integer
Private BarcodeID As Integer
Private ItemTitle As String
Private BrandTitle As String
Private ItemImg As String
Private ItemPrice As Decimal
Private TotalItemPrice As String
Private ItemQauntity As Integer
Private ItemQauntitySelected As Integer
Public Property P_ID As Integer
Get
Return Me.ID
End Get
Set
Me.ID = Value
End Set
End Property
Public Property P_ItemID As Integer
Get
Return Me.ItemID
End Get
Set
Me.ItemID = Value
End Set
End Property
Public Property P_BarcodeID As Integer
Get
Return Me.BarcodeID
End Get
Set
Me.BarcodeID = Value
End Set
End Property
Public Property P_ItemTitle As String
Get
Return Me.ItemTitle
End Get
Set
Me.ItemTitle = Value
End Set
End Property
Public Property P_BrandTitle As String
Get
Return Me.BrandTitle
End Get
Set
Me.BrandTitle = Value
End Set
End Property
Public Property P_ItemImg As String
Get
Return Me.ItemImg
End Get
Set
Me.ItemImg = Value
End Set
End Property
Public Property P_ItemPrice As Decimal
Get
Return Me.ItemPrice
End Get
Set
Me.ItemPrice = Value
End Set
End Property
Public Property P_TotalItemPrice As String
Get
Return Me.TotalItemPrice
End Get
Set
Me.TotalItemPrice = Value
End Set
End Property
Public Property P_ItemQauntity As Integer
Get
Return Me.ItemQauntity
End Get
Set
Me.ItemQauntity = Value
End Set
End Property
Public Property P_ItemQauntitySelected As Integer
Get
Return Me.ItemQauntitySelected
End Get
Set
Me.ItemQauntitySelected = Value
End Set
End Property
End Class
如果你有
Public Class Card
Property ID As Integer
Property ItemID As Integer
Property BarcodeID As Integer
Property ItemTitle As String
Property BrandTitle As String
Property ItemImg As String
Property ItemPrice As Decimal
Property TotalItemPrice As Decimal
Property ItemQuantity As Integer
Property ItemQuantitySelected As Integer
End Class
然后你可以使用List(Of Card)
来存储数据。这样做的好处是编译器知道它在其中实例化了 Card
而不仅仅是某个对象。
Dim myCookies As HttpCookie = HttpContext.Current.Request.Cookies("Mycard")
Dim cards = New List(Of Card)
For i As Integer = 0 To varCookies.Values.Count-1
Dim allValues As String() = myCookies.Values(i).Split("|"c)
Dim item = New Card
item.ItemID = Integer.Parse(allValues(0))
item.ItemTitle = allValues(1).ToString
item.BrandTitle = allValues(2).ToString
item.ItemImg = allValues(3).ToString
item.ItemPrice = Decimal.Parse(allValues(4))
'item.ItemQuantity = Integer.Parse(allValues(5))
'item.ItemQuantitySelected = Integer.Parse(allValues(6))
item.BarcodeID = Integer.Parse(allValues(7))
item.TotalItemPrice = Decimal.Parse(allValues(8))
cards.Add(item)
Next
现在编译器可以获取列表中条目的属性,您可以
Dim dataToPresent = cards.OrderBy(function(c) c.ItemId).ToList()
rptcart.DataSource = dataToPresent
rptcart.DataBind()
它会按照您选择的顺序显示数据。
如果您需要在 运行 时按不同的属性进行排序,那么搜索 "linq dynamic orderby" 应该会为您提供有用的代码。
我注意到您的 Private TotalItemPrice As String
与 item.P_TotalItemPrice=Decimal.Parse(AllValues(8))
冲突。如果你使用 Option Strict On
那么 Visual Studio 会为你指出类似的问题。
P.S。你有 Dim myCookies
但你使用 varCookies.Values.Count
。您可能想检查一下是否正确。