I have a list of expressions of type: Expression<Func<Person, bool>> and I want to aggregate them and then compile the aggregated result into a single Func<Person, bool>. I was able to create the aggregated expression but the part to compile the result aggregated expression throws an exception. Any help would be appreciated. Thank you.
Expression<Func<Person, bool>> expr1 = x => x.Age > 10;
Expression<Func<Person, bool>> expr2 = x => x.LastName == "some firstname";
Expression<Func<Person, bool>> expr3 = x => x.FirstName == "some lastname";
Expression<Func<Person, bool>> expr4 = x => x.Initial == 'a';
Expression<Func<Person, bool>> expr5 = x => x.DateOfBirth == DateTime.Now;
Expression<Func<Person, bool>> expr6 = x => x.Height > 10;
var exprList = new List<Expression<Func<Person, bool>>>()
{
expr1, expr2, expr3, expr4, expr5
};
var list = exprList
.Select(x => x.Body)
.Aggregate(Expression.AndAlso);
// this works, apparently?!
var aggregatedExpression = Expression.Lambda<Func<Person, bool>>(list, Expression.Parameter(typeof(Person), "x"));
// fails here! it cannot compile
var result = aggregatedExpression.Compile();
This is the exception:
Unhandled Exception: System.InvalidOperationException: variable 'x' of type 'TestAggregateExpression.Person' referenced from scope '', but it is not defined
at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
I believe you need to visit all expressions in the list and to replace the parameter. Use this helper:
internal sealed class ParameterReplacer : ExpressionVisitor
{
private readonly ParameterExpression _param;
private ParameterReplacer(ParameterExpression param)
{
_param = param;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node.Type == _param.Type ?
base.VisitParameter(_param) : // replace
node; // ignore
}
public static T Replace<T>(ParameterExpression param, T exp) where T : Expression
{
return (T)new ParameterReplacer(param).Visit(exp);
}
}
Usage:
var param = Expression.Parameter(typeof(Person), "x"); // I'd use 'p' by the way
exp = ParameterReplacer.Replace(param, exp);
In your case:
var list = exprList.Select(x => x.Body)
.Select(exp => ParameterReplacer.Replace(param, exp))
.Aggregate(Expression.AndAlso);
Related
I try to make my custom orderby extension method, i successfully worked my code but in addition i want to list null or empty or zero values last in result, anyone can help me about that issue ?
Here is my extension method to orderby
public static IQueryable<T> OrderBy<T>(this IQueryable<T> q, string SortField, bool isAsc)
{
//var nullExpr = Expression.Constant(null, typeof(T));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, SortField);
var exp = Expression.Lambda(prop, param);
string method = isAsc ? "OrderBy" : "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
Thanks in advance
The simplest way is to use
OrderBy(e => String.IsNullOrEmpty(e.TeamName)
This doesn't require any extension method or custom IComparer implementation etc.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => String.IsNullOrEmpty(e.TeamName))
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
Without using an extension method....
Create a custom IComparer<string> to check the empty values before using the default String.Compare. The first checks will return -1 instead of 1 or 1 instead of -1, if using the standard string comparison.
/// <summary>
/// Returns -1 instead of 1 if y is IsNullOrEmpty when x is Not.
/// </summary>
public class EmptyStringsAreLast : IComparer<string>
{
public int Compare(string x, string y)
{
if (String.IsNullOrEmpty(y) && !String.IsNullOrEmpty(x))
{
return -1;
}
else if (!String.IsNullOrEmpty(y) && String.IsNullOrEmpty(x))
{
return 1;
}
else
{
return String.Compare(x, y);
}
}
}
Pass your EmptyStringsAreLast comparer into the OrderBy of Lambda expression. In this solution teams who have entered the race should appear alphabetical order, but the unaffiliated race entries should appear at then end.
var entries = repository.Race.Where(e => e.EventId == id)
.OrderBy(e => e.TeamName, new EmptyStringsAreLast())
.ThenBy(e => e.LastName)
.ThenBy(e => e.FirstName);
This answer is perhaps what you were originally looking for - using your generic extension method:
public static IQueryable<T> OrderByFieldNullsLast<T>(this IQueryable<T> q, string SortField, bool Ascending)
{
//We are rebuilding .OrderByDescending(p => p.SortField.HasValue).ThenBy(p => p.SortField)
//i.e. sort first by whether sortfield has a value, then by sortfield asc or sortfield desc
//create the expression tree that represents the generic parameter to the predicate
var param = Expression.Parameter(typeof(T), "p");
//create an expression tree that represents the expression p=>p.SortField.HasValue
var prop = Expression.Property(param, SortField);
var hasValue = Expression.Property(prop, "HasValue");
var exp = Expression.Lambda(hasValue, param);
string method = "OrderByDescending";
Type[] types = new Type[] { q.ElementType, exp.Body.Type };
var orderByCallExpression = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
//now do the ThenBy bit,sending in the above expression to the Expression.Call
exp = Expression.Lambda(prop, param);
types = new Type[] { q.ElementType, exp.Body.Type };
method = Ascending ? "ThenBy" : "ThenByDescending";
var ThenByCallExpression = Expression.Call(typeof(Queryable), method, types,orderByCallExpression, exp);
return q.Provider.CreateQuery<T>(ThenByCallExpression);
}
Building on Dave Anson's answer, you can user Comparer.Create() to create the Comparer from a lambda. Here's an example that sorts unsorted by its myString string fields, with null or empty strings appearing last.
var sorted = unsorted.OrderBy(x => x.myString, Comparer<string>.Create((x, y) => {
if ( string.IsNullOrEmpty(y) && !string.IsNullOrEmpty(x)) return -1;
else if (!string.IsNullOrEmpty(y) && string.IsNullOrEmpty(x)) return +1;
else return string.Compare(x, y);
}))
(To put them first, switch the signs on the 1 constants)
it works for me:
private static IQueryable<T> GetOrderQuery<T>(this IQueryable<T> q, BaseFilterCollection filter)
{
q = q.OrderBy(GetExpression<T>(filter.SortField));
var param = Expression.Parameter(typeof(T), "p");
var prop = Expression.Property(param, filter.SortField);
var exp = Expression.Lambda(prop, param);
string method = filter.SortDirection == SortDirectionType.Asc ? "ThenBy" : "ThenByDescending";
Type[] types = { q.ElementType, exp.Body.Type };
var rs = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(rs);
}
private static Expression<Func<T, bool>> GetExpression<T>(string sortField)
{
ParameterExpression param = Expression.Parameter(typeof(T), "p");
Expression prop = Expression.Property(param, sortField);
var info = typeof(T).GetProperty(sortField, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
Expression exp = Expression.Equal(prop, info.PropertyType.IsValueType
? Expression.Constant(Activator.CreateInstance(info.PropertyType))
: Expression.Constant(null));
return Expression.Lambda<Func<T, bool>>(exp, param);
}
You dont need to complicate, the easiest way is to do something like this:
YourList.OrderByDescending(x => string.IsNullOrEmpty(x.value)
Use OrderByDescending or OrderBy depending on if you want to see empty strings in the beginning or last.
Regards
I'm attempting to modify an expression tree that dynamically builds a Contains expression that ultimately results in SQL like
P IN (123, 124, 125, 200, 201)
to instead check perform range checks, which ultimately results in SQL like
(P >= 123 AND P <= 125) OR (P >= 200 AND P <= 201)
I'm basing my solution on this post.
static public Expression<Func<TElement, bool>>
BuildContainsExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
// Removed for post: Input checks and edge cases
var equals =
values.Select(value =>
(Expression)Expression.Equal(valueSelector.Body,
Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate<Expression>((accumulate, equal) =>
Expression.Or(accumulate, equal));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
I'm able to get the range checking to work if I provide the value for comparison:
long testValue = 5;
List<KeyValuePair<int, int>> ranges = new List<KeyValuePair<int, int>>()
{
new KeyValuePair<long, long>(3, 6),
new KeyValuePair<long, long>(10, 12),
new KeyValuePair<long, long>(20, 20),
};
List<BinaryExpression> rangeExpressions = new List<BinaryExpression>();
foreach (var pair in ranges)
{
var greaterThanOrEqual =
Expression.GreaterThanOrEqual(Expression.Constant(testValue),
Expression.Constant(pair.Key));
var lessThanOrEqual =
Expression.LessThanOrEqual(Expression.Constant(testValue),
Expression.Constant(pair.Value));
var inRange = Expression.AndAlso(greaterThanOrEqual, lessThanOrEqual);
rangeExpressions.Add(inRange);
}
var final =
rangeExpressions.Aggregate<Expression>((a, b) => Expression.Or(a, b));
var result = Expression.Lambda<Func<bool>>(final).Compile()();
However, I cannot sort out how to get the value for comparison from the passed-in expression when I drop that code into the method to be used with Linq. The signature of that method is:
Expression<Func<TElement, bool>>
BuildRangeExpression<TElement>(
Expression<Func<TElement, long>> valueSelector,
IEnumerable<long> values)
and it is used like:
Expression<MyType, bool> match =
BuildRangeExpression<MyType, long>(my => my.ProductCode, productCodes);
var result = db.MyTypes.Where(match);
QUESTION
How can I evaluate
Expression<Func<TElement, long>> valueSelector
so that I can use the value passed into BuildRangeExpression instead of my currently hard-coded value
long testValue = 5;
I think the code from the blog post has exactly what you need: all you have to do is to use valueSelector.Body instead of your Expression.Constant() and also add the original parameter to the generated expression:
public static Expression<Func<TElement, bool>>
BuildRangeExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> valueSelector,
IEnumerable<Tuple<TValue, TValue>> values)
{
var p = valueSelector.Parameters.Single();
var equals = values.Select(
tuple =>
Expression.AndAlso(
Expression.GreaterThanOrEqual(
valueSelector.Body, Expression.Constant(tuple.Item1)),
Expression.LessThanOrEqual(
valueSelector.Body, Expression.Constant(tuple.Item2))));
var body = equals.Aggregate(Expression.OrElse);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
Use Expression.Parameter.
Create a parameter:
var param = Expression.Parameter(typeof(TElement), "arg")
Instead of Expression.Constant(testvalue), you will need to put param.
Then, you need to do:
var result = Expression.Lambda<Func<TElement, bool>>(final, param).Compile()
I currently have the following code which allows me to call any method required on the EmailAddress property of my object, and it works great:
public static Expression<Func<T, bool>> BuildEmailAddressLambda(string method, params object[] args) {
var e = Expression.Parameter(typeof(T), "e");
var propertyInfo = typeof(T).GetProperty("EmailAddress");
var m = Expression.MakeMemberAccess(e, propertyInfo);
var mi = m.Type.GetMethod(method, args.Select(a => a.GetType()).ToArray());
var c = args.Select(a => Expression.Constant(a, a.GetType())).ToArray();
Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(Expression.Call(m, mi, c), e);
return lambda;
}
// called:
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("StartsWith", "r", StringComparison.OrdinalIgnoreCase);
However, I now need to amend the code so that it works for members of my object other than EmailAddress. Specifically, I would like to create a tree to cover the following expression, where multiple method calls are used:
e.GetStringValue(12).StartsWith("r", StringComparison.OrdinalIgnoreCase);
I have made several attempts, all of which end up in various errors. I feel I am missing something in the logic of how ExpressionTrees are created and some help with this would be greatly appreciated.
Thanks
Is this good for you?
public static Expression<Func<T, bool>> BuildEmailAddressLambda<T>(
string member, IEnumerable<object> memberArgs, string method, params object[] args)
{
var e = Expression.Parameter(typeof(T), "e");
var memberInfo =
(MemberInfo) typeof(T).GetField(member) ??
(MemberInfo) typeof(T).GetProperty(member) ??
(MemberInfo) typeof(T).GetMethod(member, (memberArgs ?? Enumerable.Empty<object>()).Select(p => p.GetType()).ToArray());
Expression m;
if (memberInfo.MemberType == MemberTypes.Method)
{
var a = memberArgs.Select(p => Expression.Constant(p));
m = Expression.Call(e, (MethodInfo) memberInfo, a);
}
else
{
m = Expression.MakeMemberAccess(e, memberInfo);
}
var mi = m.Type.GetMethod(method, args.Select(a => a.GetType()).ToArray());
var c = args.Select(a => Expression.Constant(a, a.GetType()));
return Expression.Lambda<Func<T, bool>>(Expression.Call(m, mi, c), e);
}
// called:
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("EmailAddress", null, "StartsWith", "r", StringComparison.OrdinalIgnoreCase);
// or
lambda = LambdaExpressionHelper<MailingListMember>.BuildEmailAddressLambda("GetStringValue", new object[] { 12 }, "StartsWith", "r", StringComparison.OrdinalIgnoreCase);
I'm trying to dynamically create a where clause with link.
Now, creating something like SELECT Product WHERE x = 123 AND y = 349 AND w = 948 is no problem. I'm just adding extra Where filters.
products = products.Where(product => product.Property == 123);
But now i have an unknown number of values that should be tested with an OR operator. The SQL Should be like SELECT Product WHERE x = 123 OR x = 499 OR x = 998 .. and n more OR's
Build a list of numbers to check and do something like the following:
var idsToCheck = new List<int>{123, 789, 654}; //Build from dynamic list
products = products.Where(x => idsToCheck.Contains(x.Id));
The above can be duplicated for v and w if required
var idsToCheck = new List<int>{123, 789, 654}; //Build from dynamic list
products = products.Where(x => idsToCheck.Contains(x.Id));
var propertyToCheck = new List<int>{123, 789, 654};//Build from dynamic list
products = products.Where(x => propertyToCheck.Contains(x.Property));
var numberToCheck = new List<int>{123, 789, 654};
products = products.Where(x => numberToCheck.Contains(x.Number));
If your values to check are already being passed in as enumerables then you don't need to build your xxxToCheck collections. You can simply do the .Contains against the originals
You can use contains on a list or just have the equation like so:
products.Where(product => (product.Property == 123) || (product.Property == 499) || (product.Property == 998));
If you want to compose an OR query from multiple where clauses you really need to compose the predicates in the where clauses instead. Afterall, chaining where clauses would be an AND.
So, if you have the following predicates:
Func<Product, bool> w1 = p => p.x == 123;
Func<Product, bool> w2 = p => p.y == 349;
Func<Product, bool> w3 = p => p.w == 948;
Func<Product, bool> w4 = p => p.Property == 123;
Func<Product, bool> w5 = p => p.x == 499;
Func<Product, bool> w6 = p => p.x == 998;
You can define the following extension method to OR them:
public static Func<T, bool> Or<T>(this Func<T, bool> #this, Func<T, bool> that)
{
return t => #this(t) || that(t);
}
Now you could write:
var products1 = products.Where(w1.Or(w5).Or(w6));
var products2 = products.Where(
((Func<Product, bool>)(p => p.x == 123))
.Or(p => p.y == 349)
.Or(p => p.Property == 123));
If you want to work with lists of clauses then add this extension method:
public static Func<T, bool> Or<T>(this IEnumerable<Func<T, bool>> #this)
{
return #this.Aggregate((Func<T, bool>)(t => true), (a, t) => a.Or(t));
}
Now you can write:
var predicates = new List<Func<Product, bool>>()
{
p => p.x == 123,
p => p.w == 948,
p => p.Property == 123,
p => p.x == 998,
};
var products3 = products.Where(predicates.Or());
If you're writing this against an IQueryable<T> provider then you need to work with Expression<Func<Product, bool>> and not just Func<Product, bool>.
Then you need these extension methods:
public static Expression<Func<T, bool>> Or<T>(
this Expression<Func<T, bool>> #this,
Expression<Func<T, bool>> that)
{
var param =
Expression
.Parameter(typeof(T), "t");
var body =
Expression
.Or(
Expression
.Invoke(#this, param),
Expression
.Invoke(that, param));
return
Expression
.Lambda<Func<T, bool>>(body, param);
}
public static Expression<Func<T, bool>> Or<T>(
this IEnumerable<Expression<Func<T, bool>>> #this)
{
return #this.Aggregate((Expression<Func<T, bool>>)(t => true), (a, t) => a.Or(t));
}
The calling code still works the same:
var predicatesX = new List<Expression<Func<Product, bool>>>()
{
p => p.x == 123,
p => p.w == 948,
p => p.Property == 123,
p => p.x == 998,
};
var products3X = products.Where(predicatesX.Or());
Is this what you were looking for?
I'm very low experienced with Expressions in .NET, that's why I rather ask you guys.
How should I - see comment below:
using P = Myclass;
..
System.Linq.Expressions.Expression<Func<P, bool>> myExpression = null;
..
myExpression1 = x => foo1 == true && foo2 == false;
myExpression2 = x => ... ;
..
BinaryExpression resultExpression = System.Linq.Expressions.Expression.OrElse(myExpression1, myExpression2);
..
IQueryable<P> l = l.Where(?resultExpression?); // how to transform BinaryExpression to the suitable type?
Thank you
You can't "OR" lambdas together that way. You really want to "OR" the lambda bodies together. Here's a method to do that:
public static Expression<Func<T, bool>> OrTheseFiltersTogether<T>(
this IEnumerable<Expression<Func<T, bool>>> filters)
{
Expression<Func<T, bool>> firstFilter = filters.FirstOrDefault();
if (firstFilter == null)
{
Expression<Func<T, bool>> alwaysTrue = x => true;
return alwaysTrue;
}
var body = firstFilter.Body;
var param = firstFilter.Parameters.ToArray();
foreach (var nextFilter in filters.Skip(1))
{
var nextBody = Expression.Invoke(nextFilter, param);
body = Expression.OrElse(body, nextBody);
}
Expression<Func<T, bool>> result = Expression.Lambda<Func<T, bool>>(body, param);
return result;
}
Then later:
Expression<Func<P, bool>> myFilter1 = x => foo1 == true && foo2 == false;
Expression<Func<P, bool>> myFilter2 = x => ... ;
..
List<Expression<Func<P, bool>>> filters = new List<Expression<Func<P, bool>>>();
filters.Add(myfilter1);
filters.Add(myfilter2);
..
Expression<Func<P, bool>> resultFilter = filters.OrTheseFiltersTogether();
IQueryable<P> query = query.Where(resultFilter);
you might want to take a wee look at the Predicatebuilder:
http://www.albahari.com/nutshell/predicatebuilder.aspx
the Predicatebuilder allows you to run up some very powerful expressions (AND/OR/NOT etc, etc) in a very clean and easy to understand way. For simple expressions, I do of course just roll them from scratch and apply but for the complex stuff...
I'm quite a fan of it :)
a few links on SO itself that may be helpful:
LINQ to SQL PredicateBuilder
Generated SQL with PredicateBuilder, LINQPad and operator ANY
.Where method takes a lambda expression as a parameter, so you need to build your BinaryExpression to a complete LambdaExpression.
var resultExpression = Expression.OrElse(myExp1, myExp2);
// now the exp is like: p > 100 || p < 10
ParameterExpression parameterExp = Expression.Parameter(typeof(P),"p");
// construct a parameter with its type P, looks like: p =>
LambdaExpression lambdaExp = Expression.Lambda(resultExpression, parameterExp);
// merge the two expressions: p => p > 100 || p < 10
myList.Where(lambdaExp.Compile());
Its a combination of two Func<P, bool> on expression level.
A less fancy way to do the same should be:
Func<P, bool> myExpression1 = x => foo1 == true && foo2 == false;
Func<P, bool> myExpression2 = x => ... ;
IQueryable<P> l = l.Where((p) => myExpression1(p) || myExpression2(p));