This project is read-only.

Support for other operators

Aug 7, 2009 at 7:00 PM


I've managed to add "AndAlso" and "Not", and member access queries like ".Where(user=>user.IsActive)" instead of "(user=>user.IsActive == true)" operators for Where query. Just instead of previous simple check for equal operator I've added this code. I still need to do some testing, but with first tests it seems works fine. Just keep in mind, that while using AND operators, keep indexed properties on the left side. To work with both sides I still need to do more work. Maybe use joins operators between different hash lists, still I don't think that's very good option. Anyway this at least allows to use one indexed property for litle bit complex queries.
I know that these kind of queries can be done with multiple Where(..) conditions, but that could be little bit confusing, so I've wanted to have one Where condition...


var expressionBody = expr.Body;
bool isTrue = true;
var boolType = typeof(bool);
while ((expressionBody.NodeType == ExpressionType.AndAlso) || (expressionBody.Type == boolType && expressionBody.NodeType == ExpressionType.Not))
  if (expressionBody.NodeType == ExpressionType.AndAlso)
    var andExpression = (BinaryExpression)expressionBody;
    expressionBody = andExpression.Left;
  if (expressionBody.NodeType == ExpressionType.Not && expressionBody.Type == boolType)
    UnaryExpression unaryExpression = (UnaryExpression)expressionBody;
    expressionBody = unaryExpression.Operand;
    isTrue = !isTrue;

if (expressionBody.NodeType == ExpressionType.MemberAccess && expressionBody.Type == boolType)
  expressionBody =
Expression.Equal(expressionBody, Expression.Constant(isTrue));
//indexes only work on equality expressions here
if (expressionBody.NodeType == ExpressionType.Equal)
//Equality is a binary expression
BinaryExpression binExp = (BinaryExpression)expressionBody;

// everything else as it was before


Aug 9, 2009 at 6:58 PM

If you can include unit tests I think this would be a great improvement.