public static Expression NotEqualNullable(Expression e1, Expression e2) { if (e1.Type.IsNullable() == e2.Type.IsNullable()) return Expression.NotEqual(e1, e2); return Expression.NotEqual(e1.Nullify(), e2.Nullify()); }
public static Expression EqualNullable(Expression e1, Expression e2) { if (e1 == NewId || e2 == NewId) return False; if (e1.Type.IsNullable() == e2.Type.IsNullable()) return Expression.Equal(e1, e2); return Expression.Equal(e1.Nullify(), e2.Nullify()); }
public override MemberInitExpression ParameterFactory(Expression parameterName, SqlDbType sqlType, string udtTypeName, bool nullable, Expression value) { Expression valueExpr = Expression.Convert(IsDate(sqlType) ? Expression.Call(miAsserDateTime, value.Nullify()) : value, typeof(object)); if (nullable) valueExpr = Expression.Condition(Expression.Equal(value, Expression.Constant(null, value.Type)), Expression.Constant(DBNull.Value, typeof(object)), valueExpr); NewExpression newExpr = Expression.New(typeof(SqlParameter).GetConstructor(new[] { typeof(string), typeof(object) }), parameterName, valueExpr); List<MemberBinding> mb = new List<MemberBinding>() { Expression.Bind(typeof(SqlParameter).GetProperty("IsNullable"), Expression.Constant(nullable)), Expression.Bind(typeof(SqlParameter).GetProperty("SqlDbType"), Expression.Constant(sqlType)), }; if (sqlType == SqlDbType.Udt) mb.Add(Expression.Bind(typeof(SqlParameter).GetProperty("UdtTypeName"), Expression.Constant(udtTypeName))); return Expression.MemberInit(newExpr, mb); }
public static Expression GetCompareExpression(FilterOperation operation, Expression left, Expression right, bool inMemory = false) { switch (operation) { case FilterOperation.EqualTo: return Expression.Equal(left, right); case FilterOperation.DistinctTo: { var t = left.Type.UnNullify(); var mi = t.IsValueType ? miDistinctNullable : miDistinct; return Expression.Call(mi.MakeGenericMethod(t), left.Nullify(), right.Nullify()); } case FilterOperation.GreaterThan: return Expression.GreaterThan(left, right); case FilterOperation.GreaterThanOrEqual: return Expression.GreaterThanOrEqual(left, right); case FilterOperation.LessThan: return Expression.LessThan(left, right); case FilterOperation.LessThanOrEqual: return Expression.LessThanOrEqual(left, right); case FilterOperation.Contains: return Expression.Call(Fix(left, inMemory), miContains, right); case FilterOperation.StartsWith: return Expression.Call(Fix(left, inMemory), miStartsWith, right); case FilterOperation.EndsWith: return Expression.Call(Fix(left, inMemory), miEndsWith, right); case FilterOperation.Like: return Expression.Call(miLike, Fix(left, inMemory), right); case FilterOperation.NotContains: return Expression.Not(Expression.Call(Fix(left, inMemory), miContains, right)); case FilterOperation.NotStartsWith: return Expression.Not(Expression.Call(Fix(left, inMemory), miStartsWith, right)); case FilterOperation.NotEndsWith: return Expression.Not(Expression.Call(Fix(left, inMemory), miEndsWith, right)); case FilterOperation.NotLike: return Expression.Not(Expression.Call(miLike, Fix(left, inMemory), right)); default: throw new InvalidOperationException("Unknown operation {0}".Formato(operation)); } }
public override MemberInitExpression ParameterFactory(Expression parameterName, SqlDbType sqlType, string udtTypeName, bool nullable, Expression value) { Expression valueExpr = Expression.Convert(IsDate(sqlType) ? Expression.Call(miAsserDateTime, value.Nullify()) : value, typeof(object)); if (nullable) valueExpr = Expression.Condition(Expression.Equal(value, Expression.Constant(null, value.Type)), Expression.Constant(DBNull.Value, typeof(object)), valueExpr); NewExpression newExpr = Expression.New(typeof(SqlCeParameter).GetConstructor(new[] { typeof(string), typeof(object) }), parameterName, valueExpr); List<MemberBinding> mb = new List<MemberBinding>() { Expression.Bind(typeof(SqlCeParameter).GetProperty("IsNullable"), Expression.Constant(nullable)), Expression.Bind(typeof(SqlCeParameter).GetProperty("SqlDbType"), Expression.Constant(sqlType)), }; if (sqlType == SqlDbType.Udt) throw new InvalidOperationException("User Defined Tyeps not supported on SQL Server Compact ({0})".FormatWith(udtTypeName)); return Expression.MemberInit(newExpr, mb); }
public static Expression EqualNullableGroupBy(Expression e1, Expression e2) { return Expression.Or(Expression.Equal(e1.Nullify(), e2.Nullify()), Expression.And(new IsNullExpression(e1), new IsNullExpression(e2))); }