How to get alternate numbers using Enumerable.Range?

前端 未结 5 1262
梦谈多话
梦谈多话 2021-02-07 09:24

If Start=0 and Count=10 then how to get the alternate values using Enumerable.Range() the out put should be like { 0, 2, 4, 6, 8 }

相关标签:
5条回答
  • 2021-02-07 09:24
    Enumerable.Range(0, 10).Where(i => i % 2 == 0); // { 0, 2, 4, 6, 8 }
    Enumerable.Range(0, 10).Where(i => i % 2 != 0); // { 1, 3, 5, 7, 9 }
    
    0 讨论(0)
  • 2021-02-07 09:33

    What you are after here does not exist in the BCL as far as I'm aware of, so you have to create your own static class like this to achieve the required functionality:

    public static class MyEnumerable {
      public static IEnumerable<int> AlternateRange(int start, int count) {
        for (int i = start; i < start + count; i += 2) {
          yield return i;
        }
      }
    }
    

    Then you can use it like this wherever you want to:

    foreach (int i in MyEnumerable.AlternateRange(0, 10)) {
      //your logic here
    }
    

    You can then also perform LINQ queries using this since it returns IEnumerable

    So if you want you can also write the above like this if you want to exclude the number 6

    foreach (int i in MyEnumerable.AlternateRange(0, 10).Where( j => j != 6)) {
      //your logic here
    }
    

    I hope this is what you are after.

    You can't have this as an extension method on the Enumerable class directly since that is a static class, and extension methods work on an object of a class, and not the class itself. That's why you have to create a new static class to hold this method if you want to mimic the Enumerable class.

    0 讨论(0)
  • 2021-02-07 09:41

    Halving the number of items that Range should generate (its second parameter) and then doubling the resulting values will give both the correct number of items and ensure an increment of 2.

    Enumerable.Range(0,5).Select(x => x * 2)
    
    0 讨论(0)
  • 2021-02-07 09:45

    This can be done more simply using Linq and by specifying the minimum, length, and step values:

    Enumerable.Range(min, length).Where(i => (i - min) % step == 0);
    

    Usage with 0 through 10 at a step size of 2:

    var result = Enumerable.Range(0, 10).Where(i => (i - 10) % 2 == 0);
    

    Output:

    0, 2, 4, 6, 8
    

    Usage with 1 through 10 at a step size of 2:

    var result = Enumerable.Range(1, 10).Where(i => (i - 10) % 2 == 0);
    

    Output:

    1, 3, 5, 7, 9
    

    You could go further and make a simple function to output it using a minimum, maximum, and step value:

    public static IEnumerable<int> RangedEnumeration(int min, int max, int step)
    {
        return Enumerable.Range(min, max - min + 1).Where(i => (i - min) % step == 0);
    }
    

    The reason to set the range length to max - min + 1 is to ensure the max value is inclusive. If the max should be exclusive, remove the + 1.

    Usage:

    var Result = RangedEnumeration(0, 10, 2); // 0, 2, 4, 6, 8, 10
    var Result = RangedEnumeration(1, 10, 2); // 1, 3, 5, 7, 9
    var Result = RangedEnumeration(1000, 1500, 150); // 1000, 1150, 1300, 1450
    
    0 讨论(0)
  • 2021-02-07 09:48

    The count parameter in your code looks like an end point of the loop.

    public static MyExt
    {
      public static IEnumerable<int> Range(int start, int end, Func<int, int> step)
      {
        //check parameters
        while (start <= end)
        {
            yield return start;
            start = step(start);
        }
      }
    }
    

    Usage: MyExt.Range(1, 10, x => x + 2) returns numbers between 1 to 10 with step 2 MyExt.Range(2, 1000, x => x * 2) returns numbers between 2 to 1000 with multiply 2 each time.

    0 讨论(0)
提交回复
热议问题