I looking for a Between method, like the SQL Between

Oct 8, 2013 at 1:50 PM
Is anyone in this library?
Coordinator
Oct 21, 2013 at 12:17 AM
Edited Nov 6, 2013 at 11:04 PM
If what you are looking for is something like Source.Where(X=> Between 5 and 25), I cannot think of a way to implement it.

It is possible to create a between extension but it will not make things easier or more readable than a where clause:
  public static class BetweenDemo
  {
    public static IEnumerable<T> Between<T>(this IEnumerable<T> source, T value) where T : IComparable
    {
      return source.Where(X => X.CompareTo(value) >= 0);
    }

    public static IEnumerable<T> And<T>(this IEnumerable<T> source, T value) where T : IComparable
    {
      return source.Where(X => X.CompareTo(value) <= 0);
    }
  }
Quick test:
  static class Program
  {
    static void Main()
    {
      IEnumerable<int> test = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      var res = test.Between(3).And(6);
    } 
  }
the result will be 3,4,5,6


In addition there is no way to enforce the usage of the two extensions so it is possible to write something like:
test. And(6). Between(3);  
or
test.Between(3).OrderBy(…)And(6);
both will work just fine but will be confusing to say the least.
Oct 21, 2013 at 7:28 AM
Thanks, is a great solution.
But seems to force Linq to a non natural state.
I will use it in future.