Results 1 to 8 of 8

Thread: How to Pick last 1000 elements from a generic collection

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. Dim myArray(999) As String
    2.  
    3. myList.CopyTo(myList.Count - 1000, myArray, 0, 1000)
    4. myList = New List(Of String)(myArray)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to Pick last 1000 elements from a generic collection

    try this:

    vb Code:
    1. Public Class Form1
    2.     Dim integerList As New List(Of Integer)
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         For x As Integer = 1 To 10
    6.             integerList.Add(x)
    7.         Next
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Dim tempList As List(Of Integer) = integerList.Skip(8).ToList
    12.         For Each i In tempList
    13.             MsgBox(i)
    14.         Next
    15.     End Sub
    16. End Class

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to Pick last 1000 elements from a generic collection

    yeah that's what it is, cobber

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How to Pick last 1000 elements from a generic collection

    you need to rate 10 other members since last time you rated him...

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width