我在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();
    }

最佳答案

这里的问题是您定义了两个通用类型属性:TSourceTKey,但是实际上您拥有三种类型。如果TKey是与lowValuehighValue相同的类型(都是int),则可以使用,但是如果lowValuehighValue的类型是string和TKey,则不能使用仍然是int

如果我添加了第三个通用参数TLowHigh,则您提供的代码效果很好,这是您的low / hight参数的一种:

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

关于c# - Linq表达式。大于大于字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41265622/

10-11 15:51