0

Before update project to .net core (from full .net), I have used sth like that, to do And operation beetwen two expressions:

public class ExpressionBuilder<T>
{
    private Expression<Func<T, bool>> _mainExpression;

    public ExpressionBuilder()
    {
        _mainExpression = e => true;
    }

    public void AddExpression(Expression<Func<T, bool>> expression)
    {
        _mainExpression = _mainExpression.And(expression);
    }

    public Expression<Func<T, bool>> Build()
    {
        return _mainExpression;
    }
}

Now variable _mainExpression doesn't have implemented method And. What should I do to merge two expressions?

pakos
  • 1
  • 2
  • 3
  • 1
    Did this ever work? – Filip Cordas Oct 17 '16 at 11:44
  • Yes, it worked. – pakos Oct 17 '16 at 11:51
  • What framework. I tested 4.5 and 4.6 with using System.Linq; using System.Linq.Expressions; Are you sure this wasn't your custom extension method?https://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/bb335710(v=vs.110).aspx – Filip Cordas Oct 17 '16 at 12:09
  • Framework - 4.5.1, usings: System and System.Linq.Expressions. And it works correctly. But nvm, it doesn't work in .net core and that's the main problem. – pakos Oct 17 '16 at 12:15
  • There is no such method in full .net either. You are using some custom predicate builder class. – Ivan Stoev Oct 17 '16 at 12:19
  • @pakos Think about what you want this function to do. Do you really want to "and" two expressions (each requiring one parameter) and have the result also require one parameter? It doesn't make sense as a general function. If it's meant to evaluate each function with the same parameter and return the result, you can write it that way. What I mean to say is that strictly speaking, this isn't "and" but rather "compose". You are combining two boolean functions into one, whereas "and" intuitively runs two separate boolean functions. – piojo Oct 17 '16 at 12:24
  • Awww my mistake... You are right... Thanks a lot :). – pakos Oct 17 '16 at 12:26
  • Yes there is something similar in https://www.nuget.org/packages/Mono.Linq.Expressions not in .net. Mono might have it out of the box I am not sure. – Filip Cordas Oct 17 '16 at 12:53
  • 1
    You may have been using LinqKit or it's sister library PredicateBuilder (or something like that). These are also available in .Net Core. – Josh Mouch Mar 21 '17 at 14:56

1 Answers1

0

Try this:

            Expression<Func<string, bool>> exp = x => x.Contains("b");
            Expression<Func<string, bool>> exp2 = x => x.Contains("a");
            ParameterExpression param = exp.Parameters[0];
            Expression<Func<string, bool>> accumulator = Expression.Lambda<Func<string, bool>>(Expression.AndAlso(exp.Body, Expression.Invoke(exp2, param)), param);
            var flag = accumulator.Compile()("ab");
            Console.WriteLine(flag);

Also have a look over this.

Community
  • 1
  • 1
Tamas Ionut
  • 4,240
  • 5
  • 36
  • 59