public void BinaryExpression() { //Creates an instance of BinaryExpression with initial value 13 var be1 = new BinaryExpression("1101"); //be1.Length equals to 4 (number of bits) //Creates an instance of BinaryExpression with initial value 11 BinaryExpression be2 = (short)11; //be2.Length equals to 16 (number of 'short' type bits) var be2_str_2 = be2.ToString(); //Creates an instance of BinaryExpression with array of bytes constructor parameter. var be3 = new BinaryExpression(new byte[] { 128, 58, 15 }); //be3.Length equals to 24 //Gets boolean value of bit with specified index. bool be1_bit_3 = be1[2]; be1[2] = false; //be1 = 1001 = 9 int be1_be2_cmp = be1.CompareTo(be2); // = -1, 9 < 11 bool be1_IsAllTrue = be1.IsAll(true); // = false //be2 = 00000000 00001011 = 11, Length = 16 be2.Length = 10; //be2 = 00 00001011 = 11, Length = 10 be2.Trim(); //be2 = 1011 = 11, Length = 4 be1.And((byte)3); //be1 = 00000001 = 1, Length = 8 be1 |= new BinaryExpression("11110000"); // be1.Or //be1 = 11110001 = 241, Length = 8 be3 = be1 << 2; //Shift bits to the left //be3 = 11000100 = 196, Length = 8 be3 = be1.RotateLeft(2); //Shift circular bits to the left //be3 = 11000111 = 199, Length = 8 be1.Not().Trim(); //be1 = 1110 = 14, Length = 4 string binStr = be1.ToString(2); //Base: 2, 8, 10, 16 //binStr = "1110" bool[] bitValues = be1.ToArray(); //bitValues = [false, true, true, true] }
static Expression <Func <T, bool> > DynamicWhere <T>(IDictionary <string, object> conditions) { ParameterExpression pe = Expression.Parameter(typeof(T), "a"); BinaryExpression be = null; foreach (var item in conditions) { MemberExpression me = Expression.Property(pe, item.Key); ConstantExpression ce = Expression.Constant(item.Value, typeof(T).GetProperty(item.Key).PropertyType); if (be == null) { be = Expression.Equal(me, ce); } else { be = BinaryExpression.And(be, Expression.Equal(me, ce)); } } return(Expression.Lambda <Func <T, bool> >( be, new[] { pe })); }
private Expression <Func <ElementContent, bool> > buildExpression() { // Build up an Expression of a Function that accepts an Atom and returns bool that // will be used as a "where" clause. // Define an parameter "a" that will be passed into function. var elementParam = Expression.Parameter(typeof(ElementContent), "e"); Expression whereExp; // Start with a.NameSpace = NameSpace whereExp = propertyEqualEqualConst(elementParam, "NameSpace", NameSpace); // Add "&& a.Name = Name" to function if this.Name is specified. if (!string.IsNullOrWhiteSpace(Name)) { var nameEqualExp = propertyEqualEqualConst(elementParam, "Name", Name); whereExp = BinaryExpression.And(whereExp, nameEqualExp); } // Add in criteria of Pages if (Pages != null) { foreach (var page in Pages) { if (!string.IsNullOrWhiteSpace(page)) { PropertyInfo propertyInfo = typeof(ElementContent).GetProperty("Pages"); MemberExpression m = Expression.MakeMemberAccess(elementParam, propertyInfo); ConstantExpression c = Expression.Constant(page, typeof(string)); MethodInfo mi = typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) }); var e1 = Expression.Call(m, mi, c); whereExp = BinaryExpression.And(whereExp, e1); } } } // Add in criteria for dimensions if (Dimensions != null) { foreach (var dimensionCriteria in Dimensions) { // TODO: Add And Expression for metadataCriterion to whereExp if (!string.IsNullOrWhiteSpace(dimensionCriteria.DimensionName) && !string.IsNullOrWhiteSpace(dimensionCriteria.DimensionValue)) { PropertyInfo propertyInfo = typeof(ElementContent).GetProperty("Dimensions"); MemberExpression m = Expression.MakeMemberAccess(elementParam, propertyInfo); var d = new DimensionValue() { DimensionName = dimensionCriteria.DimensionName, DimensionValue = dimensionCriteria.DimensionValue }; ConstantExpression c = Expression.Constant(d, typeof(DimensionValue)); MethodInfo mi = typeof(List <DimensionValue>).GetMethod("Contains", new Type[] { typeof(DimensionValue) }); var e1 = Expression.Call(m, mi, c); whereExp = BinaryExpression.And(whereExp, e1); } } } // TODO: Add criteria for metadata // Convert the Expression to a Lambda Expression return(Expression.Lambda <Func <ElementContent, bool> >(whereExp, new ParameterExpression[] { elementParam })); }
public object CreateBusinessQueryExpression(IList <SearchingArgument> SearchingArguments) { Expression <Func <Business, bool> > expression = b => (true); if (SearchingArguments != null && SearchingArguments.Count > 0) { ParameterExpression parameter = Expression.Parameter(typeof(Business), "b"); MemberExpression member; ConstantExpression value = Expression.Constant(1); Expression body = BinaryExpression.Equal(value, value); Expression predicate = body; foreach (var arg in SearchingArguments) { member = Expression.Property(parameter, arg.FieldName); value = Expression.Constant(arg.FieldValue); switch (arg.Operator) { case OperatorEnum.EqualTo: body = BinaryExpression.Equal(member, value); break; case OperatorEnum.NotEqualTo: body = BinaryExpression.NotEqual(member, value); break; case OperatorEnum.GreaterThan: body = BinaryExpression.GreaterThan(member, value); break; case OperatorEnum.GreaterThanOrEqualTo: body = BinaryExpression.GreaterThanOrEqual(member, value); break; case OperatorEnum.In: body = Expression.Call(value, value.Type.GetInterface("ICollection`1").GetMethod("Contains"), member); break; case OperatorEnum.NotIn: body = BinaryExpression.Not(Expression.Call(value, value.Type.GetInterface("ICollection`1").GetMethod("Contains"), member)); break; case OperatorEnum.Is: break; case OperatorEnum.IsNot: break; case OperatorEnum.LessThan: body = BinaryExpression.LessThan(member, value); break; case OperatorEnum.LessThanOrEqualTo: body = BinaryExpression.LessThanOrEqual(member, value); break; case OperatorEnum.StartsWith: body = Expression.Call(member, typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), value); break; case OperatorEnum.NotStartWith: body = BinaryExpression.Not(Expression.Call(member, typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), value)); break; case OperatorEnum.EndsWith: body = Expression.Call(member, typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }), value); break; case OperatorEnum.NotEndWith: body = BinaryExpression.Not(Expression.Call(member, typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }), value)); break; case OperatorEnum.Includes: body = Expression.Call(member, member.Type.GetMethod("Contains"), value); break; case OperatorEnum.NotInclude: body = BinaryExpression.Not(Expression.Call(member, member.Type.GetMethod("Contains"), value)); break; default: break; } predicate = arg.LogicalOperator == LogicalOperatorEnum.And ? BinaryExpression.And(predicate, body) : BinaryExpression.Or(predicate, body); } expression = Expression.Lambda <Func <Business, bool> >(predicate, parameter); } return(expression); }