public static AstFilter Translate(TranslationContext context, Expression arrayFieldExpression, ConstantExpression arrayConstantExpression)
        {
            var arrayFieldTranslation = ExpressionToFilterFieldTranslator.Translate(context, arrayFieldExpression);
            var itemSerializer        = ArraySerializerHelper.GetItemSerializer(arrayFieldTranslation.Serializer);
            var values           = (IEnumerable)arrayConstantExpression.Value;
            var serializedValues = SerializationHelper.SerializeValues(itemSerializer, values);

            return(AstFilter.In(arrayFieldTranslation, serializedValues));
        }
        // public static methods
        public static AstFilter Translate(TranslationContext context, MethodCallExpression expression)
        {
            var method    = expression.Method;
            var arguments = expression.Arguments;

            if (method.Is(StringMethod.IsNullOrEmpty))
            {
                var fieldExpression = arguments[0];
                var field           = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);
                return(AstFilter.In(field, new BsonValue[] { BsonNull.Value, "" }));
            }

            throw new ExpressionNotSupportedException(expression);
        }
Example #3
0
        private static AstFilter Translate(TranslationContext context, Expression expression, Expression sourceExpression, Expression itemExpression)
        {
            if (itemExpression.NodeType == ExpressionType.Constant)
            {
                var sourceField     = ExpressionToFilterFieldTranslator.Translate(context, sourceExpression);
                var itemSerializer  = ArraySerializerHelper.GetItemSerializer(sourceField.Serializer);
                var value           = itemExpression.GetConstantValue <object>(containingExpression: expression);
                var serializedValue = SerializationHelper.SerializeValue(itemSerializer, value);
                return(AstFilter.ElemMatch(sourceField, AstFilter.Eq(AstFilter.Field("@<elem>", itemSerializer), serializedValue))); // @<elem> represents the implied element
            }

            var itemField        = ExpressionToFilterFieldTranslator.Translate(context, itemExpression);
            var sourceValues     = sourceExpression.GetConstantValue <IEnumerable>(containingExpression: expression);
            var serializedValues = SerializationHelper.SerializeValues(itemField.Serializer, sourceValues);

            return(AstFilter.In(itemField, serializedValues));
        }
        // public static methods
        public static AstFilter Translate(TranslationContext context, MethodCallExpression expression)
        {
            var method    = expression.Method;
            var arguments = expression.Arguments;

            if (method.IsOneOf(__stringInOrNinMethods))
            {
                var fieldExpression = arguments[0];
                var field           = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);

                var valuesExpression = arguments[1];
                if (valuesExpression is ConstantExpression constantValuesExpression)
                {
                    var serializedValues = new List <BsonValue>();

                    var values                      = ((IEnumerable <StringOrRegularExpression>)constantValuesExpression.Value).ToList();
                    var stringSerializer            = StringSerializer.Instance;
                    var regularExpressionSerializer = BsonRegularExpressionSerializer.Instance;
                    foreach (var value in values)
                    {
                        BsonValue serializedValue;
                        if (value?.Type == typeof(BsonRegularExpression))
                        {
                            var regularExpression = value.RegularExpression;
                            serializedValue = SerializationHelper.SerializeValue(regularExpressionSerializer, regularExpression);
                        }
                        else
                        {
                            var @string = value?.String;
                            serializedValue = SerializationHelper.SerializeValue(stringSerializer, @string);
                        }
                        serializedValues.Add(serializedValue);
                    }

                    return(method.IsOneOf(__stringInMethods) ? AstFilter.In(field, serializedValues) : AstFilter.Nin(field, serializedValues));
                }
            }

            throw new ExpressionNotSupportedException(expression);
        }