4

我今天早上看到了一个问题(在一系列值上查询我的模型),似乎由(https://stackoverflow.com/a/1447926/195550)回答,但那里的整个情况让我对更概括的兴趣解决方案。

我希望能够使用 Jon Skeet 的答案来实现一个Between可以在非 SQL 生成的环境中使用字符串键,但似乎字符串没有实现GreaterThanLessThanGreaterThanOrEqualLessThanOrEqual运算符的事实进入Linq 能够构建执行此操作所需的表达式树的方式。

  • 我意识到可以只使用这些CompareTo方法进行查询来完成这项任务,但我真的很喜欢这种优雅的query.Between(v=>v.StringKey, "abc", "hjk")表达方式。

  • 我查看了 System.Linq.Expression 程序集,发现它正在寻找一个名为“op_GreaterThan”的方法,例如用于 GreaterThan 操作,但我不知道

    1. 我是否可以为字符串实现这个(知道我不能为字符串扩展实际的“>”运算符)
    2. 如何构建正确的方法签名。
  • 我创建了以下示例并进行了测试,这些示例显示了 Between 扩展方法在字符串键上不起作用的地方。

如果可以为字符串键实现这将是非常优雅的。有人对如何实现这一目标有任何建议或见解吗?

来自 Jon Skeet 的运算符之间,添加了包含标志


public static class BetweenExtension
{
    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 lowerBound = (inclusive)
                  ? Expression.GreaterThanOrEqual(key, Expression.Constant(low))
                  : Expression.GreaterThan(key, Expression.Constant(low));

        var upperBound = (inclusive)
                  ? Expression.LessThanOrEqual(key, Expression.Constant(high))
                  : Expression.LessThan(key, Expression.Constant(high));

        var and = Expression.AndAlso(lowerBound, upperBound);
        var lambda = Expression.Lambda<Func<TSource, bool>>(
                        and, keySelector.Parameters);

        return source.Where(lambda);
    }
}

使用“int”键对上述内容进行工作测试


[TestFixture]
public class BetweenIntTests
{
    public class SampleEntityInt
    {
        public int SampleSearchKey { get; set; }
    }

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

    [Test]
    public void BetweenIntInclusive()
    {
        var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
        var query = sampleData.Between(s => s.SampleSearchKey, 3, 10);
        Assert.AreEqual(2, query.Count());
    }

    [Test]
    public void BetweenIntNotInclusive()
    {
        var sampleData = BuildSampleEntityInt(1, 3, 10, 11, 12, 15);
        var query = sampleData.Between(s => s.SampleSearchKey, 2, 11, false);
        Assert.AreEqual(2, query.Count());
    }
}

使用“字符串”键对上述内容进行非工作测试


[TestFixture]
public class BetweenStringsTests
{

    public class SampleEntityString
    {
        public string SampleSearchKey { get; set; }
    }

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

    [Test]
    public void BetweenStringInclusive()
    {
        var sampleData = BuildSampleEntityString(1, 3, 10, 11, 12, 15);
        var query = sampleData.Between(s => s.SampleSearchKey, "3", "10");
        Assert.AreEqual(2, query.Count());
    }

    [Test]
    public void BetweenStringNotInclusive()
    {
        var sampleData = BuildSampleEntityString(1, 3, 10, 11, 12, 15);
        var query = sampleData.Between(s => s.SampleSearchKey, "2", "11", false);
        Assert.AreEqual(2, query.Count());
    }
}
4

1 回答 1

2

您必须调用该string.CompareTo方法作为表达式树的一部分。然后,您可以测试其结果。要查看它的外观,请查看调试器中的以下值:

Expression<<Func<string, bool>> filter = str => str.CompareTo("abc") > 0;
于 2013-02-15T18:17:45.823 回答