PredicateBuilder

    0

    0

    The code creates an Expression that tests whether the customer id matches a given value. If the state variable is not null, the code creates a lambda expression that uses the property or field name State to find the corresponding value in the state variable. The lambda expression then uses a constant value to represent the current state of the state variable. The code returns the lambda expression.

    public Expression<Func<Customer, bool>> BuildExpression()
    {
      Expression<Func<Customer, bool>> predicate = c => c.Id == CustomerId;
    
      if (State != null)
      {
        var parameter = predicate.Parameters.First();
        var newBody = Expression.AndAlso(
          predicate.Body,
          Expression.Equal(
            Expression.PropertyOrField(parameter, nameof(State)),
            Expression.Constant(State)
          ));
    
        predicate = Expression.Lambda<Func<Customer, bool>>(newBody, parameter);
      }
    
      return predicate;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.