Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. trying to skip and jump elements in a list

trying to skip and jump elements in a list

Scheduled Pinned Locked Moved LINQ
20 Posts 6 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C cechode

    perhaps i'm totally not getting it then. TakeWhile will still only return sequential items that will return true in the expression so skip(5).TakeWhile(....) will only return a list with one item ( number 5 ) for #6 will fail. assuming i have a list 1,2,3,4,5,6,7,8,9 and i write a for(int i=2; i<9, i+=3) this list will return 2,5,8 takewhile will return just 2

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #9

    TakeWhile and SkipWhile both take a predicate which you can code to return the appropriate elements.


    I know the language. I've read a book. - _Madmatt

    1 Reply Last reply
    0
    • C cechode

      brain fart here :( how do you convert this to link for (int i = 5; i < Count; i+=7) { MyList.Add(this[i]); }

      V Offline
      V Offline
      Vincent Blais
      wrote on last edited by
      #10

      Try this,

      var result = this.Skip(5).TakeWhile((i, index) => (index % 7) == 0);

      Vince Remember the dead, fight for the living

      C 1 Reply Last reply
      0
      • V Vincent Blais

        Try this,

        var result = this.Skip(5).TakeWhile((i, index) => (index % 7) == 0);

        Vince Remember the dead, fight for the living

        C Offline
        C Offline
        cechode
        wrote on last edited by
        #11

        many thanks for your answer, but it does not do the trick. your takewhile will stop on the first failure. and given a list 0...50 it will only return one item ( 6 ) first pass is (0%7)==0 (true) second pass is (1%7)==0 (false) where the expected result would be 6,13,20,27....... it'd be sweet if it could recuse and call skip and take 1 every 7 for now i ended up writing a custom GetEnumerator to return the desired indexes ( but i hate it)

        D 1 Reply Last reply
        0
        • C cechode

          many thanks for your answer, but it does not do the trick. your takewhile will stop on the first failure. and given a list 0...50 it will only return one item ( 6 ) first pass is (0%7)==0 (true) second pass is (1%7)==0 (false) where the expected result would be 6,13,20,27....... it'd be sweet if it could recuse and call skip and take 1 every 7 for now i ended up writing a custom GetEnumerator to return the desired indexes ( but i hate it)

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #12

          try this:

          var needed = this.Where((i, index) => ((index == 5 || ((index - 5) % 7 == 0)))).ToList();

          Should work. Although the credit goes to Vince. TakeWhile or SkipWhile stops at the first false statement. Where doesn't. :)

          I bug

          modified on Monday, August 9, 2010 2:54 PM

          V C 2 Replies Last reply
          0
          • D Dan Mos

            try this:

            var needed = this.Where((i, index) => ((index == 5 || ((index - 5) % 7 == 0)))).ToList();

            Should work. Although the credit goes to Vince. TakeWhile or SkipWhile stops at the first false statement. Where doesn't. :)

            I bug

            modified on Monday, August 9, 2010 2:54 PM

            V Offline
            V Offline
            Vincent Blais
            wrote on last edited by
            #13

            You're right Moshu,

            Vince Remember the dead, fight for the living

            D 1 Reply Last reply
            0
            • V Vincent Blais

              You're right Moshu,

              Vince Remember the dead, fight for the living

              D Offline
              D Offline
              Dan Mos
              wrote on last edited by
              #14

              About the credit part? I was joking ;P

              I bug

              V 1 Reply Last reply
              0
              • D Dan Mos

                try this:

                var needed = this.Where((i, index) => ((index == 5 || ((index - 5) % 7 == 0)))).ToList();

                Should work. Although the credit goes to Vince. TakeWhile or SkipWhile stops at the first false statement. Where doesn't. :)

                I bug

                modified on Monday, August 9, 2010 2:54 PM

                C Offline
                C Offline
                cechode
                wrote on last edited by
                #15

                i was down that path between posts here but the one thing that bugged me most about these solutions was that i had to traverse all elements in the list to find the ones i wanted ( every 7th in this case ) and though i agree with the comment you made a few posts ago about the actual cost here i still got a bit annoyed with it ;) anyhoo, i tried 2 options 1. generate a list of the desired index numbers and do my From clause from this list and my select clause would be something like select this[currentitem] but had a hard time generating a list like 5,12,19,26.... to do my FROM caluse on.

                from idx in indexlist(5,12,19,26...)
                select this[idx]

                2. create ForI extension which was not to my liking but was happier with that than that annoying other "cost" :) i decided to create a ForI extension

                    public static System.Collections.Generic.IEnumerable ForI(this IEnumerable source, int start, int skip)
                    {
                        int idx = start;
                        while (idx < source.Count())
                        {
                            yield return source.ElementAt(idx);
                            idx += skip;
                        }
                    }
                

                // so i could do something like
                List nums = new List();
                int q = 0;
                while ((q++) <; 50)
                nums.Add(q);

                        foreach (var item in nums.ForI(5,7))
                        {
                            Console.WriteLine(item);
                        }
                

                nonetheless i appreciate your comments, made me not give up looking thanks

                V D 2 Replies Last reply
                0
                • D Dan Mos

                  About the credit part? I was joking ;P

                  I bug

                  V Offline
                  V Offline
                  Vincent Blais
                  wrote on last edited by
                  #16

                  Lol :-D

                  Vince Remember the dead, fight for the living

                  1 Reply Last reply
                  0
                  • C cechode

                    brain fart here :( how do you convert this to link for (int i = 5; i < Count; i+=7) { MyList.Add(this[i]); }

                    E Offline
                    E Offline
                    englebart
                    wrote on last edited by
                    #17

                    Borrowed some from Mark N. I am ignorant on this API, but it seems that a pseudo code structure like this might also work. You could probably stuff it into a function that takes the initial Skip and the distance bewteen elements.

                    var collection = new collection();
                    var search = (from x in this select x);
                    search.Skip(5);
                    while (search.hasNext()){
                    collection.add(search.Take(1));
                    // might want to replace this with a loop of search.Skip(1) due to exceptions?
                    if (search.hasNext())
                    search.Skip(7-1);
                    }

                    1 Reply Last reply
                    0
                    • C cechode

                      i was down that path between posts here but the one thing that bugged me most about these solutions was that i had to traverse all elements in the list to find the ones i wanted ( every 7th in this case ) and though i agree with the comment you made a few posts ago about the actual cost here i still got a bit annoyed with it ;) anyhoo, i tried 2 options 1. generate a list of the desired index numbers and do my From clause from this list and my select clause would be something like select this[currentitem] but had a hard time generating a list like 5,12,19,26.... to do my FROM caluse on.

                      from idx in indexlist(5,12,19,26...)
                      select this[idx]

                      2. create ForI extension which was not to my liking but was happier with that than that annoying other "cost" :) i decided to create a ForI extension

                          public static System.Collections.Generic.IEnumerable ForI(this IEnumerable source, int start, int skip)
                          {
                              int idx = start;
                              while (idx < source.Count())
                              {
                                  yield return source.ElementAt(idx);
                                  idx += skip;
                              }
                          }
                      

                      // so i could do something like
                      List nums = new List();
                      int q = 0;
                      while ((q++) <; 50)
                      nums.Add(q);

                              foreach (var item in nums.ForI(5,7))
                              {
                                  Console.WriteLine(item);
                              }
                      

                      nonetheless i appreciate your comments, made me not give up looking thanks

                      V Offline
                      V Offline
                      Vincent Blais
                      wrote on last edited by
                      #18

                      Nice solution cechode I would try to extract the Count() call of the loop because Count() can be costly over large collection. Count() will use the Count property if available(ICollection) but will iterate the source sequence otherwise. This will force you to traverse your sequence more time then the Where clause. Can be a problem if the source is allowed to change before the end of ForI.

                      cechode wrote:

                              public static System.Collections.Generic.IEnumerable ForI(this IEnumerable source, int start, int skip)
                              {
                                    int idx = start;
                                    int count = source.Count()
                                    while (idx < count)
                                    {
                                          yield return source.ElementAt(idx);
                                          idx += skip;
                                    }
                              }
                      

                      Vince Remember the dead, fight for the living

                      1 Reply Last reply
                      0
                      • C cechode

                        brain fart here :( how do you convert this to link for (int i = 5; i < Count; i+=7) { MyList.Add(this[i]); }

                        F Offline
                        F Offline
                        Fernando Soto
                        wrote on last edited by
                        #19

                        Hi cechode; The code snippet below will generate a sequence of integers that start at 5 and adds 12 to each previous value and will end with a value of 100 or less.

                        IEnumerable<int> index = Enumerable.Range(5, 100).Where(idx => (idx % 7) == 5 && idx <= 100);

                        Fernando

                        1 Reply Last reply
                        0
                        • C cechode

                          i was down that path between posts here but the one thing that bugged me most about these solutions was that i had to traverse all elements in the list to find the ones i wanted ( every 7th in this case ) and though i agree with the comment you made a few posts ago about the actual cost here i still got a bit annoyed with it ;) anyhoo, i tried 2 options 1. generate a list of the desired index numbers and do my From clause from this list and my select clause would be something like select this[currentitem] but had a hard time generating a list like 5,12,19,26.... to do my FROM caluse on.

                          from idx in indexlist(5,12,19,26...)
                          select this[idx]

                          2. create ForI extension which was not to my liking but was happier with that than that annoying other "cost" :) i decided to create a ForI extension

                              public static System.Collections.Generic.IEnumerable ForI(this IEnumerable source, int start, int skip)
                              {
                                  int idx = start;
                                  while (idx < source.Count())
                                  {
                                      yield return source.ElementAt(idx);
                                      idx += skip;
                                  }
                              }
                          

                          // so i could do something like
                          List nums = new List();
                          int q = 0;
                          while ((q++) <; 50)
                          nums.Add(q);

                                  foreach (var item in nums.ForI(5,7))
                                  {
                                      Console.WriteLine(item);
                                  }
                          

                          nonetheless i appreciate your comments, made me not give up looking thanks

                          D Offline
                          D Offline
                          Dan Mos
                          wrote on last edited by
                          #20

                          hey that's a nice solution. thanks for posting it :)

                          I bug

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups