我试图添加到列表中。如果我将声明移动到一个循环中它工作正常但速度太慢,如果我不这样做,添加方法会覆盖以前的值。
我该怎么办?
Dim _obj As New ClassDAL.Site.SalesPoint
Dim _r = _obj.GetCities
Dim temp As New ClassDAL.Site.SalesPoint
Dim myList As New List(Of ClassDAL.Site.SalesPoint)
For Each row In _r
temp.FullCode = Convert.ToString(row.Item(0))
temp.CityCode = Convert.ToString(row.Item(1))
temp.Karm = Convert.ToInt32(row.Item(2))
temp.Name = Convert.ToString(row.Item(3))
myList.Add(temp)
temp = New ClassDAL.Site.SalesPoint
Next
Return myList
答案 0 :(得分:1)
你必须在循环中声明你的变量,因为它是一个引用类型,否则在下一个循环中你将修改对你创建的最后一个项的引用。
你可以尝试这样做,但我认为它不会更快:
For Each row In _r
myList.Add(New ClassDAL.Site.SalesPoint With {
.FullCode = Convert.ToString(row.Item(0))
.CityCode = Convert.ToString(row.Item(1))
.Karm = Convert.ToInt32(row.Item(2))
.Name = Convert.ToString(row.Item(3))
})
Next