I'm having a hard time creating an expression builder function to pass to a query for NHibernate repository.
Basically what I want to be able to do is build a query from various object properties (strings) and compare their value to values in an array. Also be able to add other query sentences.
Something like
public static Expression<Func<TElement, bool>> BuildOrExpression<TElement, TValue>
(Dictionary<Expression<Func<TElement, TValue>>, IEnumerable<TValue>> expressionSet)
{
List<Expression> newExpressions = new List<Expression>();
foreach (var item in expressionSet)
{
if (null == item.Key) throw new ArgumentNullException("valueSelector");
if (null == item.Value) throw new ArgumentNullException("values");
ParameterExpression p = item.Key.Parameters.Single();
if (!item.Value.Any()) return e => false;
IEnumerable<Expression> expressions = item.Value
.Select(value => (Expression)Expression.Equal(item.Key.Body, Expression.Constant(value, typeof(TValue))));
Expression compExpression = expressions
.Aggregate<Expression>((accumulate, equal) => Expression.OrElse(accumulate, equal));
newExpressions.Add(Expression.Lambda<Func<TElement, bool>>(compExpression, p));
}
Expression accExpression = newExpressions
.Aggregate<Expression>((accumulate, equal) => Expression.OrElse(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(accExpression);
}
But instead of doing the Expression.Equal
somehow doing a String.Contains
.
Any help welcomed. By the way the code above is not tested.
Instead of Expression.Equal
you need to use Expression.Call
and pass Contains MethodInfo instance. Something like this
IEnumerable<Expression> expressions = item.Value.Select(value =>
(Expression)Expression.Call(item.Key.Body,
typeof(String).GetMethod("Contains"),
Expression.Constant(value, typeof(TValue))));
I was able to overcome the expression combining problem but Im not sure this is the best solution. Any who I used the solution provided by Vladimir and PredicateBuilder by Joe Albahari (C# 4 book writer). This is what I came up with.
public static Expression<Func<TElement, bool>> BuildOrExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
if (null == valueSelector) throw new ArgumentNullException("valueSelector");
if (null == values) throw new ArgumentNullException("values");
ParameterExpression p = valueSelector.Parameters.Single();
if (!values.Any()) return e => false;
Expression<Func<string, string, bool>> expFunc = (name, value) => name.Contains(value);
IEnumerable<Expression> equals = values.Select(value => (Expression)Expression.Call(valueSelector.Body, typeof(String).GetMethod("Contains"), Expression.Constant(value, typeof(TValue))));
Expression body = equals.Aggregate<Expression>((accumulate, equal) => Expression.OrElse(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
public static Expression<Func<T, bool>> CombineOr<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> CombineAnd<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
[TestMethod()]
public void OfferComplexSearchTest()
{
using (var lifetime = container.BeginLifetimeScope())
{
IOfferRepository offerRepository = lifetime.Resolve<IOfferRepository>();
List<Offer> offers = new List<Offer>();
List<Expression<Func<Offer, bool>>> expressions = new List<Expression<Func<Offer, bool>>>();
Expression<Func<Offer, bool>> finalExp = null;
string[] orQuery = new string[2] { "director".ToUpper(), "jefe".ToUpper() };
expressions.Add(LinqTools.BuildOrExpression<Offer, string>(c => c.Description.ToUpper(), orQuery));
expressions.Add(LinqTools.BuildOrExpression<Offer, string>(c => c.Title.ToUpper(), orQuery));
expressions.Add(LinqTools.BuildOrExpression<Offer, string>(c => c.Keywords.ToUpper(), orQuery));
finalExp = expressions.Aggregate<Expression<Func<Offer, bool>>>((accomulate, equal) => LinqTools.CombineOr<Offer>(accomulate, equal));
offers.AddRange(offerRepository.GetMany(LinqTools.CombineAnd<Offer>(finalExp, (x) => x.Publish)));
Assert.IsTrue(offers.Count > 0, "Error: No Offers found.");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.