I have two linq expressions I want to combine but my code gives me an error The binary operator And is not defined for the types 'System.Func`2[Web.Entities.Customer,System.Boolean]' and 'System.Func`2[Web.Entities.Customer,System.Boolean]'.
I have 2 expressions for example...
Expression<Func<Customer, bool>> filter = c => c.Active;
Expression<Func<Customer, bool>> filterz = c => c.Visible;
i then combine them
filter = Expression.Lambda<Func<Customer, bool>>(Expression.And(filter, filterz));
Any help regarding this problem?
thanks....
here is the updateed code given on the answer below.
public class SwapVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public SwapVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
filter = Expression.Lambda<Func<Customer, bool>>(Expression.AndAlso(
new SwapVisitor(filter.Parameters[0], filterz.Parameters[0]).Visit(filter.Body), filterz.Body), filterz.Parameters)
Expression<Func<Customer, bool>> filter1 = c => c.Active;
Expression<Func<Customer, bool>> filter2 = c => c.Visible;
var parameter = Expression.Parameter(typeof(Customer), "x");
var filter = Expression.Lambda<Func<Customer, bool>>(
Expression.AndAlso(
Expression.Invoke(filter1,parameter),
Expression.Invoke(filter2,parameter)
),parameter
);
if you are working with the same Customer in each filter you have, than you can try:
Expression<Func<Customer, bool>> filter = c => c.Active;
Expression<Func<Customer, bool>> filter2 = c => c.Visible;
var body = Expression.AndAlso(filter.Body, Expression.Invoke(filter2, filter.Parameters[0]));
filter = Expression.Lambda<Func<Customer, bool>>(body, filter.Parameters);
var applyFilter = filter.Compile();
var customer = new Customer() { Visible = true, Active = true};
Console.WriteLine(applyFilter(customer));
customer.Active = false;
Console.WriteLine(applyFilter(customer));
customer.Visible = false;
Console.WriteLine(applyFilter(customer));
You already can use your expressions in repository without outer combining them:
var yourContext = getContext();
var filtered = yourContext.Where(filter).Where(filter2);
Combining is unnecessary in this case and this approach will work either without affecting to efficiency.
If you need combining:
Try use the following visitor helper:
public class ReplacementVisitor : System.Linq.Expressions.ExpressionVisitor
{
private readonly Expression _oldExpr;
private readonly Expression _newExpr;
public ReplacementVisitor(Expression oldExpr, Expression newExpr)
{
_oldExpr = oldExpr;
_newExpr = newExpr;
}
public override Expression Visit(Expression node)
{
if (node == _oldExpr)
return _newExpr;
return base.Visit(node);
}
}
We need it because, your filtering expressions use different parameters instances. For combining we need them to use the same parameter instance. This class helps us to do the following:
Expression<Func<Customer, bool>> filter = c => c.Active;
Expression<Func<Customer, bool>> filterz = c => c.Visible;
var newParameter = Expression.Parameter(typeof(Customer), "x");
var visitor1 = new ReplacementVisitor(filter.Parameters[0], newParameter);
var visitor2 = new ReplacementVisitor(filterz.Parameters[0], newParameter);
var newLambda = Expression.Lambda(
Expression.AndAlso(
visitor1.Visit(filter.Body),
visitor2.Visit(filterz.Body)
),
newParameter
);
Related
I am having a problem with converting a string to property.
Example of what do I have:
Expression<Func<string, bool>> expression = (a => a == "Hello");
What do I want:
Expression<Func<Entity, bool>> expression = (a => a.Name == "Hello");
However, I need the mentioned method to do the following as well:
Expression<Func<string, bool>> expression = (a => "Hello" == a);
=>
Expression<Func<Entity, bool>> expression = (a => "Hello" == a.Name);
What I think might work, but I am unable to do it:
I think that a visitor could be a way how to properly do this. However, I am not sure how to properly handle it.
You need to create new lambda expression where parameter has type Entity and body is a body of original expression where string parameter replaced by accessor to Name property. ExpressionVisitor is a right tool to do such replacement
// parameter for new lambda
var p = Expression.Parameter(typeof(Entity), "p");
// Expression for p.Name
var exprProp = Expression.Property(p, nameof(Entity.Name));
// replace parameter in original expression
var visitor = new ReplaceParamVisitor(expression.Parameters[0], exprProp);
var exprNewBody = visitor.Visit(expression.Body);
// create new lambda
var lambda = Expression.Lambda<Func<Entity, bool>>(exprNewBody, p);
Visitor:
public class ReplaceParamVisitor: ExpressionVisitor
{
private readonly ParameterExpression param;
private readonly Expression replacement;
public ReplaceParamVisitor(ParameterExpression param, Expression replacement)
{
this.param = param;
this.replacement = replacement;
}
protected override Expression VisitParameter(ParameterExpression node) => node == param ? replacement : node;
}
I need to concatenate two expressions (with or statement)
My code:
var items = new List<Item>
{
new Item { Color = "Black", Categories = new List<string> { "cat1", "cat2" } },
new Item { Color = "Red", Categories = new List<string> { "cat3" } },
new Item { Color = "White", Categories = new List<string> { "cat1" } }
};
var categories = new List<string> { "cat2", "cat3" };
Expression<Func<Item, bool>> func1 = (x1) => x1.Color == "Black";
Expression<Func<Item, bool>> func2 = (x2) => x2.Categories.Any(y => categories.Where(z => z == y).Any());
Expression<Func<Item, bool>> fullExpression = Expression.Lambda<Func<Item, bool>>(
Expression.Or(func1.Body, func2.Body), func1.Parameters.Single());
var result = items.AsQueryable().Where(fullExpression);
// result should be like this
// items.Where(x => (x.Color == "Black") || x.Categories.Any(y => categories.Where(z => z == y).Any()))
I get run-time error variable 'x2' of type 'Item' referenced from scope '', but it is not defined'
I also was trying to build an expression with ExpressionVisitor.
Here is ExpressionVisitor:
internal class ParameterReplacer : ExpressionVisitor
{
private readonly ParameterExpression _parameter;
internal ParameterReplacer(ParameterExpression parameter)
{
_parameter = parameter;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return base.VisitParameter(_parameter);
}
}
Here how I use it in code:
Expression<Func<Item, bool>> func1 = (x1) => x1.Color == "Black";
Expression<Func<Item, bool>> func2 = (x2) => x2.Categories.Any(y => categories.Select(z => z == y).Any());
var paramExpr = Expression.Parameter(typeof(Item));
var exprBody = Expression.Or(func1.Body, func2.Body);
exprBody = (BinaryExpression)new ParameterReplacer(paramExpr).Visit(exprBody);
var finalExpr = Expression.Lambda<Func<Item, bool>>(exprBody, paramExpr);
var result = items.AsQueryable().Where(finalExpr);
In this case during creating ParameterReplacer I'm getting error
System.InvalidOperationException: 'The operands for operator 'Equal' do not match the parameters of method 'op_Equality'.'
What did I do wrong?
Ian Newson is entirely right but if you want code, here you go :)
Using these two classes you can combine the two predicates. I didn't come up with it but improved/adjusted it a bit and made it use the type Predicate instead of Func along with some newer language features (the original was quite old, sadly I don't remember where I found it).
internal class SubstExpressionVisitor : ExpressionVisitor
{
private readonly Dictionary<Expression, Expression> _subst = new Dictionary<Expression, Expression>();
protected override Expression VisitParameter(ParameterExpression node)
{
if (_subst.TryGetValue(node, out Expression newValue))
{
return newValue;
}
return node;
}
public Expression this[Expression original]
{
get => _subst[original];
set => _subst[original] = value;
}
}
public static class PredicateBuilder
{
// you don't seem to need this but it's included for completeness sake
public static Expression<Predicate<T>> And<T>(this Expression<Predicate<T>> a, Expression<Predicate<T>> b)
{
if (a == null)
throw new ArgumentNullException(nameof(a));
if (b == null)
throw new ArgumentNullException(nameof(b));
ParameterExpression p = a.Parameters[0];
SubstExpressionVisitor visitor = new SubstExpressionVisitor();
visitor[b.Parameters[0]] = p;
Expression body = Expression.AndAlso(a.Body, visitor.Visit(b.Body));
return Expression.Lambda<Predicate<T>>(body, p);
}
public static Expression<Predicate<T>> Or<T>(this Expression<Predicate<T>> a, Expression<Predicate<T>> b)
{
if (a == null)
throw new ArgumentNullException(nameof(a));
if (b == null)
throw new ArgumentNullException(nameof(b));
ParameterExpression p = a.Parameters[0];
SubstExpressionVisitor visitor = new SubstExpressionVisitor();
visitor[b.Parameters[0]] = p;
Expression body = Expression.OrElse(a.Body, visitor.Visit(b.Body));
return Expression.Lambda<Predicate<T>>(body, p);
}
}
You can use it like this:
Expression<Predicate<Item>> func1 = (x1) => x1.Color == "Black";
Expression<Predicate<Item>> func2 = (x2) => x2.Categories.Any(y => categories.Select(z => z == y).Any());
Expression<Predicate<Item>> finalExpr = func1.Or(func2);
You might want to keep in mind that my Or is using OrElse internally so the second expression won't be evaluated if the one before is evaluated to true (OrElse is like ||, not |). The same goes for And with AndAlso (AndAlso is like &&, not &).
Another thing to note is that you can easily replace Predicate<T> with Func<T, bool> if you have to use Func for some reason :)
This is because your two expressions (func1 and func2) reference two different ParameterExpressions. Just because they're of the same type doesn't mean they're the same.
They need to be the exact same ParameterExpression instance for this to work. For that you can leverage an expression rewriter to modify one of the expressions: https://learn.microsoft.com/en-us/dotnet/api/system.linq.expressions.expressionvisitor?view=netframework-4.8
I think you should be able to use a library like predicate builder to do the same thing in a simpler way though:
https://www.nuget.org/packages/PredicateBuilder/
EDIT:
Your code is on the right lines, but make the following changes:
For the visitor:
internal class ParameterReplacer : ExpressionVisitor
{
private readonly ParameterExpression _parameter;
internal ParameterReplacer(ParameterExpression parameter)
{
_parameter = parameter;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return node;
}
}
For the execution bit:
Expression<Func<Item, bool>> func1 = (x1) => x1.Color == "Black";
Expression<Func<Item, bool>> func2 = (x2) => x2.Categories.Any(y => categories.Select(z => z == y).Any());
var paramExpr = func1.Parameters.Single();
var expr2 = new ParameterReplacer(paramExpr).Visit(func1);
var exprBody = Expression.Or(func1.Body, ((LambdaExpression)expr2).Body);
var finalExpr = Expression.Lambda<Func<Item, bool>>(exprBody, paramExpr);
var result = items.AsQueryable().Where(finalExpr)
.ToList();
All is said in the title, more precisely I am searching a way to convert an
Expression<Func<TDerived, out bool>> to Expression<Func<TBase, out bool>>,
with TDerived deriving from TBase.
How can I achieve this?
Given an Expression replacer like this one:
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
// A simple expression visitor to replace some nodes of an expression
// with some other nodes. Can be used with anything, not only with
// ParameterExpression
public class SimpleExpressionReplacer : ExpressionVisitor
{
public readonly Dictionary<Expression, Expression> Replaces;
public SimpleExpressionReplacer(Expression from, Expression to)
{
Replaces = new Dictionary<Expression, Expression> { { from, to } };
}
public SimpleExpressionReplacer(Dictionary<Expression, Expression> replaces)
{
// Note that we should really clone from and to... But we will
// ignore this!
Replaces = replaces;
}
public SimpleExpressionReplacer(IEnumerable<Expression> from, IEnumerable<Expression> to)
{
Replaces = new Dictionary<Expression, Expression>();
using (var enu1 = from.GetEnumerator())
using (var enu2 = to.GetEnumerator())
{
while (true)
{
bool res1 = enu1.MoveNext();
bool res2 = enu2.MoveNext();
if (!res1 || !res2)
{
if (!res1 && !res2)
{
break;
}
if (!res1)
{
throw new ArgumentException("from shorter");
}
throw new ArgumentException("to shorter");
}
Replaces.Add(enu1.Current, enu2.Current);
}
}
}
public override Expression Visit(Expression node)
{
Expression to;
if (node != null && Replaces.TryGetValue(node, out to))
{
return base.Visit(to);
}
return base.Visit(node);
}
}
now we can, given
public class Base
{
public int ValueBase { get; set; }
}
public class Derived : Base
{
public int ValueDerived { get; set; }
}
and a
Expression<Func<Derived, bool>> exp = x => x.ValueBase == 0;
then
ParameterExpression parOld = exp.Parameters[0];
ParameterExpression parNew = Expression.Parameter(typeof(Base));
// Replace the parOld with the parNew
Expression body2 = new SimpleExpressionReplacer(parOld, parNew).Visit(exp.Body);
// Note that we have to rebuild the Expression.Lambda<>
Expression<Func<Base, bool>> expNew = Expression.Lambda<Func<Base, bool>>(body2, parNew);
This will produce a
Expression<Func<Base, bool>> exp = x => x.ValueBase == 0;
Note that if you want instead to do:
Expression<Func<Derived, bool>> exp = x => x.ValueDerived == 0;
to
Expression<Func<Base, bool>> exp = x => ((Derived)x).ValueDerived == 0;
then you need something like:
ParameterExpression parOld = exp.Parameters[0];
ParameterExpression parNew = Expression.Parameter(typeof(Base));
UnaryExpression convert = Expression.Convert(parNew, typeof(Derived));
Expression body2 = new SimpleExpressionReplacer(parOld, convert).Visit(exp.Body);
Expression<Func<Base, bool>> expNew = Expression.Lambda<Func<Base, bool>>(body2, parNew);
You need to wrap the inner expression. Something like
var argument = Expression.Parameter(typeof(TDerived));
Expression.Lambda<Func<TDerived, bool>>
(
Expression.Invoke(innerExpression, argument),
argument
);
Of course, depending on the direction, you might need an explicit cast on the argument to innerExpression - this is quite simple, just use Expression.Cast.
EDIT:
To accomodate for your edit, the inverted variant:
var argument = Expression.Parameter(typeof(TBase));
Expression.Lambda<Func<TBase, bool>>
(
Expression.Invoke(innerExpression, Expression.Convert(argument, typeof(TDerived))),
argument
);
Note that this will obviously only work if the runtime type of the parameter is derived from TDerived.
I have an expression tree for Purchase class
public static readonly Expression<Func<Purchase, double?>> CurrentPaidSumLambda =
p => (double?)p.Payments
.Where(pa => pa.Status == SystemConstants.PaymentStatus.Paid)
.Sum(pa => pa.Sum);
I wish to create projection into a type PurchaseSummaryInfo like this
var ps = Db.Purchases.Select(p =>
new PurchaseSummaryInfo
{
paidSum = (double?)p.Payments
.Where(pa => pa.Status == SystemConstants.PaymentStatus.Paid)
.Sum(pa => pa.Sum) ?? 0
});
But to use my "pre-canned" expression tree.
Can it be done, and if yes - how?
We'll need a Combine method. This method will take an expression that accepts a value and computes an intermediate result, and then a second expression that accepts the same input as the first expression, accepts the intermediate result as a second parameter, and then computes a new value.
public static Expression<Func<TFirstParam, TResult>>
Combine<TFirstParam, TIntermediate, TResult>(
this Expression<Func<TFirstParam, TIntermediate>> first,
Expression<Func<TFirstParam, TIntermediate, TResult>> second)
{
var param = Expression.Parameter(typeof(TFirstParam), "param");
var newFirst = first.Body.Replace(first.Parameters[0], param);
var newSecond = second.Body.Replace(second.Parameters[0], param)
.Replace(second.Parameters[1], newFirst);
return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
As an implementation, it relies on the ability to replace all instances of one expression with another, which can be done using this:
internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}
public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}
So now we can write:
var ps = Db.Purchases.Select(CurrentPaidSumLambda.Combine((p, sum) =>
new PurchaseSummaryInfo
{
paidSum = sum ?? 0
});
I have a variable number of OR conditions that I want to put together into one Linq query.
How do I do this in a loop? Basically, the final query is to be:
IQueryable<MyObject> Q;
Q = Q.Where(q => (condition1) || (condition2) || ..... || (condition N));
Something like:
For (int i = 0; i < someNumber; i++) {
Q = Q.Where(q => (existing conditions) || (q.Value == i));
}
What statement can I use to replace (existing condition) in example above without having the final expression (Q) have nested Q's inside them?
Thanks.
You'd need to build an expression tree representing all the conditions you were interested in, combined with Expression.OrElse, and then call Where a single time at the end.
This may be somewhat tricky if your current source is an anonymous type, but it shouldn't be too bad otherwise. Here's a sample - there may be a simpler way of doing the parameter replacement, but this isn't too bad. (Although ExpressionVisitor only works in .NET 4... you'd have to implement something similar yourself if you wanted to use this in .NET 3.5.)
using System;
using System.Linq;
using System.Linq.Expressions;
public class Test
{
static void Main()
{
IQueryable<string> strings = (new[] { "Jon", "Tom", "Holly",
"Robin", "William" }).AsQueryable();
Expression<Func<string, bool>> firstPredicate = p => p.Contains("ll");
Expression<Func<string, bool>> secondPredicate = p => p.Length == 3;
Expression combined = Expression.OrElse(firstPredicate.Body,
secondPredicate.Body);
ParameterExpression param = Expression.Parameter(typeof(string), "p");
ParameterReplacer replacer = new ParameterReplacer(param);
combined = replacer.Visit(combined);
var lambda = Expression.Lambda<Func<string, bool>>(combined, param);
var query = strings.Where(lambda);
foreach (string x in query)
{
Console.WriteLine(x);
}
}
// Helper class to replace all parameters with the specified one
class ParameterReplacer : ExpressionVisitor
{
private readonly ParameterExpression parameter;
internal ParameterReplacer(ParameterExpression parameter)
{
this.parameter = parameter;
}
protected override Expression VisitParameter
(ParameterExpression node)
{
return parameter;
}
}
}
A less-than-optimized version (pray that the backend will do the necessary lifting and optimization).
public static IQueryable<T> Any<T>(this IQueryable<T> q,
params Expression<Func<T, bool>>[] preds)
{
var par = Expression.Parameter(typeof(T), "x");
Expression body = Expression.Constant(false);
foreach (var pred in preds)
{
body = Expression.OrElse(body, Expression.Invoke(pred, par));
}
var ff = Expression.Lambda(body, par) as Expression<Func<T, bool>>;
return q.Where(ff);
}
static void Main(string[] args)
{
var q = new[] { "jim", "bob", "Jon", "leppie" }.AsQueryable();
Expression<Func<string, bool>>[] preds =
{
x => x == "Jon",
x => x == "Skeet",
x => x == "leppie"
};
var result = q.Any(preds).ToArray();
}
public static IEnumerable<T> GetItemsThatMatchAny<T> (this IEnumerable<T> source, IEnumerable<Func<T,bool>> predicates)
{
return source.Where(t => predicates.Any(predicate => predicate(t)));
}
An example of a predicate generator:
private static IEnumerable<Func<MyClass, bool>> GetPredicates (int num)
{
var predicates = new Func<MyClass, bool>[] {m => m.Foo == 3, m => m.Bar =="x", m => DateTime.Now.DayOfWeek == DayOfWeek.Sunday};
return predicates.Take (num);
}