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(
            Expression.PropertyOrField(parameter, nameof(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.