今天早上我看到了一个问题(Query my model on a range of values)似乎由(https://stackoverflow.com/a/1447926/195550)回答,但整个情况让我对更广泛的解决方案感兴趣。
我希望能够使用Jon Skeet的答案来实现一个Between
,它可以在非SQL生成环境中使用字符串键,但看起来字符串没有实现 GreaterThan , LessThan , GreaterThanOrEqual , LessThanOrEqual 运算符阻碍了Linq能够构建必要的表达式树此
我意识到可以使用CompareTo
方法进行查询来完成此任务,但我真的很喜欢query.Between(v=>v.StringKey, "abc", "hjk")
表达式的优雅。
我查看了System.Linq.Expression程序集,发现它正在寻找名为'op_GreaterThan'的方法,例如对于GreaterThan操作,但我不知道< / em>的
我创建了以下示例和测试,显示了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());
}
}
答案 0 :(得分:1)
您必须在表达式树中调用string.CompareTo
方法。然后,您可以测试其结果。要查看它的外观,请在调试器中查看以下值:
Expression<<Func<string, bool>> filter = str => str.CompareTo("abc") > 0;