locked
LINQ with ArrayList

    Question

  • I have an arraylist with say 100 items in it and I want to remove [n] ranges out of it

    like index 2 thru 6
    and then index 23 thru 77

    Wz the best way to do utlizing LINQ?

    I am for now iterating through the array and marking the items as NULL in that desired range
    and then doing array.Where(val=>val!=NULL), it works but this solution is using 2 passes, i to mark them null second to remove nulls!!
    Looking for smarter/faster way!

    TIA


    SV
    Tuesday, May 11, 2010 5:01 PM

Answers

All replies

  • LINQ stands for Language Integrated Query. I don't know why you think it can delete elements in an array.

    You can use linq to return another array, however it faster to just write a for loop to do a full scan from such a small array.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Tuesday, May 11, 2010 5:26 PM
  • Because you can scan in 1 statement instead of doing the conventional for loop

    SV
    Tuesday, May 11, 2010 6:14 PM
  • By remove I mean Purge/Delete

    SV
    Tuesday, May 11, 2010 6:15 PM
  • Yes, you can scan and save the results in a new array, but not deleting from the old array. You can write your own code to do that, expose as an extension method if you want to write it in one line, but Linq to Object isn't capable for deleting on its own. All it know is that its data source implements IEnumerable.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Tuesday, May 11, 2010 6:31 PM
  • array = array.Where((x, i) => (i >= 2 && i <= 6) || (i >= 23 && i <= 77))).ToArray();

    Just don't tell anyone I told you. ;)

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Marked as answer by vai2000 Wednesday, May 12, 2010 12:17 PM
    Tuesday, May 11, 2010 6:41 PM