[英]Linq Expressions. Equivalent of greater than for strings
我在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());
我收到以下錯誤:
{“未為類型'System.String'和'System.Int32'定義二進制運算符GreaterThanOrEqual。“}
當以下行運行時。
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();
}
這里的問題是,您定義了兩個通用類型屬性: TSource
和TKey
,但實際上您擁有三種類型。 如果TKey
與lowValue
和highValue
類型相同(都為int
),則可以使用,但如果lowValue
和highValue
的類型為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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.