How to Pick last 1000 elements from a generic collection
Say a collection grow. Once it's size reach 10k, I want to get rid the first 9k. How to do so?
Oh ya I could use removeRange.
Is there another function that will output say, the last 1k of the element, say if I do not want to remove anything? Something like tail or something.
Re: How to Pick last 1000 elements from a generic collection
Assuming .NET 3.5 or later, you have LINQ at your disposal:
vb.net Code:
myList = myList.Skip(myList.Count - 1000).ToList()
That will skip all but the last 1000 items, then create a new List from the rest.
If you're using an earlier version, or even if you're not, you can use the CopyTo method that every collection has:
vb.net Code:
Dim myArray(999) As String
myList.CopyTo(myList.Count - 1000, myArray, 0, 1000)
myList = New List(Of String)(myArray)
Re: How to Pick last 1000 elements from a generic collection
try this:
vb Code:
Public Class Form1
Dim integerList As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x As Integer = 1 To 10
integerList.Add(x)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tempList As List(Of Integer) = integerList.Skip(8).ToList
For Each i In tempList
MsgBox(i)
Next
End Sub
End Class
Re: How to Pick last 1000 elements from a generic collection
It must be the cold over there that has made your fingers too slow to keep up Paul. ;)
Re: How to Pick last 1000 elements from a generic collection
yeah that's what it is, cobber:D
Re: How to Pick last 1000 elements from a generic collection
:) But then again, I can no longer rate JM because he helped me so much. Maybe if I give rating to everyone it'll work again as usual occasionally.
Re: How to Pick last 1000 elements from a generic collection
you need to rate 10 other members since last time you rated him...
Re: How to Pick last 1000 elements from a generic collection
I know. To be frank, 10 is kind of unfair because again and again JMcilhinney is the one that helped me. The least I can do for now is spell his name correctly. Not Anglo Saxon aren't you JM?