title: Linq Expressions. Equivalent of greater than for strings
body: <p>I have followed several threads on SO and other websites and am still having difficulty.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>public static IQueryable&lt;TSource&gt; Between&lt;TSource, TKey&gt;(this IQueryable&lt;TSource&gt; source, Expression&lt;Func&lt;TSource, TKey&gt;&gt; keySelector, TKey low, TKey high, bool inclusive = true) where TKey : IComparable&lt;TKey&gt;
      {
          var key = Expression.Invoke(keySelector, keySelector.Parameters.ToArray());
  
          var intLow = int.Parse(low.ToString());
          var intHigh = int.Parse(high.ToString());
  
          var lowerBound = (inclusive)
                    ? Expression.GreaterThanOrEqual(key, Expression.Constant(intLow, typeof(int)))
                    : Expression.GreaterThan(key, Expression.Constant(intLow, typeof(int)));
  
          var upperBound = (inclusive)
                    ? Expression.LessThanOrEqual(key, Expression.Constant(intHigh, typeof(int)))
                    : Expression.LessThan(key, Expression.Constant(intHigh, typeof(int)));
  
          var and = Expression.AndAlso(lowerBound, upperBound);
          var lambda = Expression.Lambda&lt;Func&lt;TSource, bool&gt;&gt;(
                          and, keySelector.Parameters);
  
          return source.Where(lambda);
      }
  </code></pre>
  
  <p>If I call this function with the following code:</p>
  
  <pre><code>lowValue = 2;
  highValue = 11;
  
  var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
  var query = sampleData.Between(s =&gt; s.SampleSearchKey, lowValue, highValue, false);
  Assert.AreEqual(2, query.Count());
  </code></pre>
  
  <p>It works. But If I use Strings instead like:</p>
  
  <pre><code>lowValueString = "3";
  highValueString = "10";
  
  var sampleData = BuildSampleEntityString(1, 3, 10, 11, 12, 15);
  var query = sampleData.Between(s =&gt; s.SampleSearchKey, lowValueString , highValueString);
  Assert.AreEqual(2, query.Count());
  </code></pre>
  
  <p>or convert the strings to integers first</p>
  
  <pre><code>lowValueString = "3";
  highValueString = "10";
  int.TryParse(lowValueString, out lowValue);
  int.TryParse(highValueString, out highValue);
  
  
  var sampleData = BuildSampleEntityString(1, 3, 10, 11, 12, 15);
  var query = sampleData.Between(s =&gt; s.SampleSearchKey, lowValue, highValue);
  Assert.AreEqual(2, query.Count());
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>{"The binary operator GreaterThanOrEqual is not defined for the types
    'System.String' and 'System.Int32'."}</p>
  </blockquote>
  
  <p>When the following line runs.</p>
  
  <pre><code>var lowerBound = (inclusive)
               ? Expression.GreaterThanOrEqual(key, Expression.Constant(intLow, typeof(int)))
               : Expression.GreaterThan(key, Expression.Constant(intLow, typeof(int)));
  </code></pre>
  
  <p>Can anyone point out what needs to be changed?</p>
  
  <p>I have added the following:</p>
  
  <pre><code> private IQueryable&lt;SampleEntityInt&gt; BuildSampleEntityInt(params int[] values)
      {
          return values.Select(
                 value =&gt;
                 new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
      }
  </code></pre>