0
votes

We use filters at work which are a type

Expression < Func < T, bool > >

which we use with methods like:

Tuple< string, string, object > SplitFilter< People >(x => x.SurName == "Smith");

I need to make a method that takes a Expression< Func < T, bool > > as a parameter, like shown above, and breaks it down into 3 values, a string "SurName" for the property, a string "==" to represent an equality comparison, and a string "Smith" for the value being compared too.

I managed to get the string "SurName" out of it, but I can't for the life of my figure out how to determine the type of comparison (equality) or the compared value ("Smith")

2

2 Answers

1
votes

Got it working the other day. The end goal was actually to then hash these values, so we can get a hash value out of each filter. For future people, how I broke it down in the end was like so:

private int HashFilter<T>( Expression<Func<T, bool>> filter ) {
  var param = filter.Parameters[0];
  var operation = filter.Body as BinaryExpression;
  var leftParameter = operation.Left as ParameterExpression;
  var leftIndex = operation.Left as MemberExpression;
  var type = operation.Left.GetType().FullName;
  var rightConstant = operation.Right as ConstantExpression;
  object result;
  if ( rightConstant == null ) {
    var rightMember = operation.Right as MemberExpression;
    result = Expression.Lambda( rightMember ).Compile().DynamicInvoke();
  }
  else {
    result = rightConstant.Value;
  }
  var value = result as string;
  var leftHashCode = leftParameter != null ? leftParameter.Name.GetStableHashCode() : leftIndex.Member.Name.GetStableHashCode();
  var operationHashCode = operation.NodeType.ToString().GetStableHashCode();
  unchecked {
    if ( value != null ) {
      return leftHashCode | operationHashCode | value.GetStableHashCode();
    }
    else {
      return leftHashCode | operationHashCode | result.GetHashCode();
    }
  }
}

With GetStableHashCode being the following extension hasing algorithm (because hashing a string uses the reference, so "Hi".GetHashCode() == "Hi".GetHashCode() would never evaluate to true, but this GetStableHashCode would.

public static int GetStableHashCode( this string str ) {
  unchecked {
    int hash1 = (5381 << 16) + 5381;
    int hash2 = hash1;

    for ( int i = 0; i < str.Length; i += 2 ) {
      hash1 = ((hash1 << 5) + hash1) ^ str[i];
      if ( i == str.Length - 1 )
        break;
      hash2 = ((hash2 << 5) + hash2) ^ str[i + 1];
    }

    return hash1 + (hash2 * 1566083941);
  }
}
0
votes

Taken from here: https://www.codeproject.com/Questions/322211/Parsing-a-linq-expression

static readonly List<int> data = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

static IEnumerable<int> SelectData(Expression<Predicate<int>> selector)
{
    ParameterExpression param = (ParameterExpression)selector.Parameters[0];
    BinaryExpression operation = (BinaryExpression)selector.Body;
    ParameterExpression left = (ParameterExpression)operation.Left;
    ConstantExpression right = (ConstantExpression)operation.Right;

    Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
                      param.Name, left.Name, operation.NodeType, right.Value);
    //...

    return from d in data where selector.Compile()(d) select d;
}

static void Main(string[] args)
{
    Console.WriteLine("data = {0}", string.Join(",", SelectData(d=>d>4)));
}

Decomposed expression: d => d GreaterThan 4
data = 5,6,7,8,9