private SqlExpression BuildKeyFilter(PersistRequestBuilderContext context, SqlTableRef filteredTable, List <PersistParameterBinding> currentBindings)
        {
            SqlExpression result = null;

            foreach (var column in context.PrimaryIndex.KeyColumns.Keys)
            {
                if (!context.ParameterBindings.TryGetValue(column, out var binding))
                {
                    var typeMapping = driver.GetTypeMapping(column);
                    var fieldIndex  = GetFieldIndex(context.Type, column);
                    binding = new PersistParameterBinding(typeMapping, fieldIndex);
                    context.ParameterBindings.Add(column, binding);
                }
                result &= filteredTable[column.Name] == binding.ParameterReference;
                currentBindings.Add(binding);
            }
            return(result);
        }
        private SqlExpression VisitParameterAccess(Expression e, bool smartNull)
        {
            var type = e.Type;

            // In rare cases (when calculated column is just parameter access) we need to strip cast to object.
            if (e.NodeType == ExpressionType.Convert && e.Type == typeof(object))
            {
                type = ((UnaryExpression)e).Operand.Type;
            }
            bool optimizeBooleanParameter = type == typeof(bool);

            type = type.StripNullable();
            var typeMapping = driver.GetTypeMapping(type);
            var expression  = parameterExtractor.ExtractParameter <object>(e);
            var bindingType = optimizeBooleanParameter
        ? QueryParameterBindingType.BooleanConstant
        : (smartNull
            ? QueryParameterBindingType.SmartNull
            : QueryParameterBindingType.Regular);
            var           binding = RegisterParameterBinding(typeMapping, expression, bindingType);
            SqlExpression result;

            if (optimizeBooleanParameter)
            {
                result = SqlDml.Variant(binding, SqlFalse, SqlTrue);
                if (fixBooleanExpressions)
                {
                    result = booleanExpressionConverter.IntToBoolean(result);
                }
            }
            else
            {
                result = binding.ParameterReference;
                if (type == typeof(bool) && fixBooleanExpressions)
                {
                    result = booleanExpressionConverter.IntToBoolean(result);
                }
                else if (typeMapping.ParameterCastRequired)
                {
                    result = SqlDml.Cast(result, typeMapping.MapType());
                }
            }
            return(result);
        }