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. Using Linq Expression.GreatThan on Strings

Using Linq Expression.GreatThan on Strings

Scheduled Pinned Locked Moved LINQ
linqcsharpdatabasefunctionalquestion
2 Posts 2 Posters 14 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.
  • P Offline
    P Offline
    pgr_home
    wrote on last edited by
    #1

    I am using to build a set of functions that will allow me to build complex queries within Linq. So far I have the following code:

    private IQueryable BuildSampleEntityInt(params int\[\] values)
    {
        return values.Select(
               value =>
               new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
    }
    
    public static IQueryable Between(this IQueryable source, Expression\> keySelector, TKey low, TKey high, bool inclusive = true) where TKey : IComparable
    {
        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\>(
                        and, keySelector.Parameters);
    
        return source.Where(lambda);
    }
    
    private IQueryable BuildSampleEntityInt(params int\[\] values)
    {
        return values.Select(
               value =>
               new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
    }
    

    If I use the following code

     lowValue = 2;
     highValue = 11;
    
     var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
     var query = sampleData.Between(s => s.SampleSearchKey, lowValue, highValue, false);
     Assert.AreEqual(2, query.Count());
    

    All is well, but if I use:

    lowValueString = "3";
    highValueString = "10";
    
    var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
    var query = sampleData.Between(s => s.SampleSearchKey, lowValueString , highValueString);
    Assert.AreEqual(2, query.Count());
    

    The code crashes because the methods; GreaterThan, LessThan, GreaterThanOrEqual and LessThanOrEqual of an Expression does not work with Strings.

    M 1 Reply Last reply
    0
    • P pgr_home

      I am using to build a set of functions that will allow me to build complex queries within Linq. So far I have the following code:

      private IQueryable BuildSampleEntityInt(params int\[\] values)
      {
          return values.Select(
                 value =>
                 new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
      }
      
      public static IQueryable Between(this IQueryable source, Expression\> keySelector, TKey low, TKey high, bool inclusive = true) where TKey : IComparable
      {
          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\>(
                          and, keySelector.Parameters);
      
          return source.Where(lambda);
      }
      
      private IQueryable BuildSampleEntityInt(params int\[\] values)
      {
          return values.Select(
                 value =>
                 new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
      }
      

      If I use the following code

       lowValue = 2;
       highValue = 11;
      
       var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
       var query = sampleData.Between(s => s.SampleSearchKey, lowValue, highValue, false);
       Assert.AreEqual(2, query.Count());
      

      All is well, but if I use:

      lowValueString = "3";
      highValueString = "10";
      
      var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
      var query = sampleData.Between(s => s.SampleSearchKey, lowValueString , highValueString);
      Assert.AreEqual(2, query.Count());
      

      The code crashes because the methods; GreaterThan, LessThan, GreaterThanOrEqual and LessThanOrEqual of an Expression does not work with Strings.

      M Offline
      M Offline
      Maciej Los
      wrote on last edited by
      #2

      If you would like to implement CompareTo() method, check this: [IComparable.CompareTo Method (Object) (System)](https://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx). GreaterThan() and LessThan() methods won't work on strings. Why? It's simple. String is not numeric value! To be able to compare lowValueString and highValueString with the set of numbers, you need to convert string values into proper data type. Convertion should be made before you data into Between method.

      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