Dynamic linq Binary and/or operator (Nhibernate linq)

dynamic-linq nhibernate

Question

I want to create an expression that as a result is a binary and or a binary or operation. Something like

int a = 1;
int b = 2;
int c = 3;
int d = a & b; // d = 0
int e = a & c; // e = 1

I hav not found anything in dynamic linq! But in linq this is a normal expression!

public class Customer{
    [Flags]
    public enum Status{
        None = 0,
        Blocked = 1,
        Present = 2,
        Paid = 4
    }

// Holds the numeric status from database.
    public int CustomerStatusValue{get; set;}
}



var listOfCustomers = new List<Customer>(); // Would load from db...
// This works in linq
var result = listOfCustomers.Where(c => (c.CustomerStatusValue & 3) != 0); 

// This does not work!!!
var result = listOfCustomers.AsQueryable()..Where("(CustomerStatusValue & 3) != 0"); 
1
1
5/10/2017 10:59:10 AM

Popular Answer

Based on Frédéric's suggestion, I downloaded the System.Dynamic.Linq file and changed it for '&' and '|' operatiors:

  // +, -, &, | operators
  Expression ParseAdditive()
  {
     Expression left = ParseMultiplicative();
     while (token.id == TokenId.Plus || token.id == TokenId.Minus ||
         token.id == TokenId.Amphersand || token.id == TokenId.Bar)
     {
        Token op = token;
        NextToken();
        Expression right = ParseMultiplicative();
        switch (op.id)
        {
           case TokenId.Plus:
              if (left.Type == typeof(string) || right.Type == typeof(string))
                 goto case TokenId.Amphersand;
              CheckAndPromoteOperands(typeof(IAddSignatures), op.text, ref left, ref right, op.pos);
              left = GenerateAdd(left, right);
              break;
           case TokenId.Minus:
              CheckAndPromoteOperands(typeof(ISubtractSignatures), op.text, ref left, ref right, op.pos);
              left = GenerateSubtract(left, right);
              break;
           case TokenId.Amphersand:
              if (IsNumericType(left.Type) && IsNumericType(right.Type))
              {
                 left = GenerateBinaryAnd(left, right);
              }
              else{
                 left = GenerateStringConcat(left, right);
              }
              break;
           case TokenId.Bar:
              if (IsNumericType(left.Type) && IsNumericType(right.Type))
              {
                 left = GenerateBinaryOr(left, right);
              }
              else
              {
                 left = GenerateStringConcat(left, right);
              }
              break;
        }
     }
     return left;
  }


  Expression GenerateBinaryAnd(Expression left, Expression right)
  {
     return Expression.MakeBinary(ExpressionType.And, left, right);
  }

  Expression GenerateBinaryOr(Expression left, Expression right)
  {
     return Expression.MakeBinary(ExpressionType.Or, left, right);
  }

I replaced the ParseAdditive function and created two new funtions that make a binary operation. So things like .Where("(a & B) > 0") now wioll work!!!!

It is not a final version because there are missing error checks and the name of the function 'ParseAdditive' maybe is not the right place or name, but it works.

1
5/10/2017 10:59:24 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow