protected override Expression VisitConstant(ConstantExpression node) { if (node.Value == null) { Out("null"); } else { LinqParameterContainer container = node.Value as LinqParameterContainer; if (container != null) { string stringValue = container.Property as string; if (stringValue != null) { Out("\"" + stringValue + "\""); } else { Out(container.Property.ToString()); } } else { Out(node.Value.ToString()); } } return(node); }
public void Parameterize_ProducesPropertyAccessOnConstant(object value) { Expression expr = LinqParameterContainer.Parameterize(value.GetType(), value); LinqParameterContainer parameterizedValue = ((expr as MemberExpression).Expression as ConstantExpression).Value as LinqParameterContainer; Assert.Equal(value, parameterizedValue.Property); }
public static Expression Parameterize(Type type, object value) { // () => new LinqParameterContainer(constant).Property // instead of returning a constant expression node, wrap that constant in a class the way compiler // does a closure, so that EF can parameterize the constant (resulting in better performance due to expression translation caching). LinqParameterContainer containedValue = LinqParameterContainer.Create(type, value); return(Expression.Property(Expression.Constant(containedValue), "TypedProperty")); }