Linq表达。相当于大于字符串

时间:2016-12-21 14:53:04

标签: c# linq lambda expression linq-expressions

我已经在SO和其他网站上关注了几个主题,但仍然遇到了困难。

我有以下代码:

public static IQueryable<TSource> Between<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, TKey low, TKey high, bool inclusive = true) where TKey : IComparable<TKey>
    {
        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<Func<TSource, bool>>(
                        and, keySelector.Parameters);

        return source.Where(lambda);
    }

如果我使用以下代码调用此函数:

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());

有效。但是如果我使用Strings而不是:

lowValueString = "3";
highValueString = "10";

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

或首先将字符串转换为整数

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 => s.SampleSearchKey, lowValue, highValue);
Assert.AreEqual(2, query.Count());

我收到以下错误:

  

{“没有为类型定义二元运算符GreaterThanOrEqual   'System.String'和'System.Int32'。“}

当以下行运行时。

var lowerBound = (inclusive)
             ? Expression.GreaterThanOrEqual(key, Expression.Constant(intLow, typeof(int)))
             : Expression.GreaterThan(key, Expression.Constant(intLow, typeof(int)));

有人能指出需要改变的内容吗?

我添加了以下内容:

 private IQueryable<SampleEntityInt> BuildSampleEntityInt(params int[] values)
    {
        return values.Select(
               value =>
               new SampleEntityInt() { SampleSearchKey = value }).AsQueryable();
    }

1 个答案:

答案 0 :(得分:3)

这里的问题是您定义了两个泛型类型属性:TSourceTKey,但实际上您有三种类型。如果TKey的类型与lowValuehighValue相同(两者都是int),那么它可以正常工作,但如果lowValue并且它不起作用highValue类型为string,TKey仍为int

您提供的代码工作正常,如果我添加第三个通用参数TLowHigh,那就是您的低/高参数的类型:

public static IQueryable<TSource> Between<TSource, TKey, TLowHigh>(
      this IQueryable<TSource> source, 
      Expression<Func<TSource, TKey>> keySelector, 
      TLowHigh low, 
      TLowHigh high,
      bool inclusive = true) 
          where TKey : IComparable<TKey>
{
    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<Func<TSource, bool>>(
                    and, keySelector.Parameters);

    return source.Where(lambda);
}