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
P

pgr_home

@pgr_home
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

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

    LINQ linq csharp database functional question
  • Login

  • Don't have an account? Register

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