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");
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.