<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities.Reflection;
using Signum.Entities.Properties;
using System.Collections;

namespace Signum.Entities.DynamicQuery
{
    [Serializable]
    public class Filter
    {
        QueryToken token;
        public QueryToken Token { get { return token; } }

        FilterOperation operation;
        public FilterOperation Operation { get { return operation; } }

        object value;
        public object Value { get { return value; } }

        public Filter(QueryToken token, FilterOperation operation, object value)
        {
            this.token = token;
            this.operation = operation;
            this.value = ReflectionTools.ChangeType(value, operation == FilterOperation.IsIn ? typeof(IEnumerable<>).MakeGenericType(Token.Type.Nullify()) : Token.Type);
        }

        static MethodInfo miContainsEnumerable = ReflectionTools.GetMethodInfo((IEnumerable<int> s) => s.Contains(2)).GetGenericMethodDefinition();
        static MethodInfo miContains = ReflectionTools.GetMethodInfo((string s) => s.Contains(s));
        static MethodInfo miStartsWith = ReflectionTools.GetMethodInfo((string s) => s.StartsWith(s));
        static MethodInfo miEndsWith = ReflectionTools.GetMethodInfo((string s) => s.EndsWith(s));
        static MethodInfo miLike = ReflectionTools.GetMethodInfo((string s) => s.Like(s));

        public Expression GetCondition(BuildExpressionContext context)
        {
            List<CollectionElementToken> allAny = Token.FollowC(a => a.Parent)
                .OfType<CollectionElementToken>()
                .Where(a => a.CollectionElementType == CollectionElementType.Any ||
                            a.CollectionElementType == CollectionElementType.All).ToList();

            if (allAny.IsEmpty())
                return GetConditionBasic(context);

            var parameters = allAny.ToDictionary(a => a, a => a.CreateParameter());
            var expressions = allAny.ToDictionary(a => (QueryToken)a, a => a.CreateExpression(parameters[a]));

            context.Replacemens.AddRange(expressions);

            Expression exp = GetConditionBasic(context);

            foreach (CollectionElementToken cet in allAny)
            {
                LambdaExpression lambda = Expression.Lambda(exp, parameters[cet]);

                exp = cet.BuildExpressionLambda(context, lambda);
            }

            context.Replacemens.RemoveRange(expressions.Keys);

            return exp;
        }

        private Expression GetConditionBasic(BuildExpressionContext context)
        {
            Expression left = Token.BuildExpression(context);

            if (Operation == FilterOperation.IsIn)
            {
                IList clone =(IList)Activator.CreateInstance(Value.GetType(), Value);

                bool hasNull = false;
                while (clone.Contains(null))
                {
                    clone.Remove(null);
                    hasNull = true;
                }

                Expression right = Expression.Constant(clone, typeof(MList<>).MakeGenericType(Token.Type.Nullify()));
                var contains =  Expression.Call(miContainsEnumerable.MakeGenericMethod(Token.Type.Nullify()), right, left.Nullify());

                if (!hasNull)
                    return contains;

                return Expression.Or(Expression.Equal(left, Expression.Constant(null, Token.Type.Nullify())), contains);
            }
            else
            {<commit_msg>Fix Filter in DynamicQuery<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using Signum.Utilities;
using Signum.Utilities.ExpressionTrees;
using Signum.Utilities.Reflection;
using Signum.Entities.Properties;
using System.Collections;

namespace Signum.Entities.DynamicQuery
{
    [Serializable]
    public class Filter
    {
        QueryToken token;
        public QueryToken Token { get { return token; } }

        FilterOperation operation;
        public FilterOperation Operation { get { return operation; } }

        object value;
        public object Value { get { return value; } }

        public Filter(QueryToken token, FilterOperation operation, object value)
        {
            this.token = token;
            this.operation = operation;
            this.value = ReflectionTools.ChangeType(value, operation == FilterOperation.IsIn ? typeof(IEnumerable<>).MakeGenericType(Token.Type.Nullify()) : Token.Type);
        }

        static MethodInfo miContainsEnumerable = ReflectionTools.GetMethodInfo((IEnumerable<int> s) => s.Contains(2)).GetGenericMethodDefinition();
        static MethodInfo miContains = ReflectionTools.GetMethodInfo((string s) => s.Contains(s));
        static MethodInfo miStartsWith = ReflectionTools.GetMethodInfo((string s) => s.StartsWith(s));
        static MethodInfo miEndsWith = ReflectionTools.GetMethodInfo((string s) => s.EndsWith(s));
        static MethodInfo miLike = ReflectionTools.GetMethodInfo((string s) => s.Like(s));

        public Expression GetCondition(BuildExpressionContext context)
        {
            List<CollectionElementToken> allAny = Token.FollowC(a => a.Parent)
                .OfType<CollectionElementToken>()
                .Where(a => a.CollectionElementType == CollectionElementType.Any ||
                            a.CollectionElementType == CollectionElementType.All).ToList();

            if (allAny.IsEmpty())
                return GetConditionBasic(context);

            var parameters = allAny.ToDictionary(a => a, a => a.CreateParameter());
            var expressions = allAny.ToDictionary(a => (QueryToken)a, a => a.CreateExpression(parameters[a]));

            context.Replacemens.AddRange(expressions);

            Expression exp = GetConditionBasic(context);

            foreach (CollectionElementToken cet in allAny)
            {
                LambdaExpression lambda = Expression.Lambda(exp, parameters[cet]);

                exp = cet.BuildExpressionLambda(context, lambda);
            }

            context.Replacemens.RemoveRange(expressions.Keys);

            return exp;
        }

        private Expression GetConditionBasic(BuildExpressionContext context)
        {
            Expression left = Token.BuildExpression(context);

            if (Operation == FilterOperation.IsIn)
            {
                IList clone =(IList)Activator.CreateInstance(Value.GetType(), Value);

                bool hasNull = false;
                while (clone.Contains(null))
                {
                    clone.Remove(null);
                    hasNull = true;
                }

                Expression right = Expression.Constant(clone, typeof(IEnumerable<>).MakeGenericType(Token.Type.Nullify()));
                var contains =  Expression.Call(miContainsEnumerable.MakeGenericMethod(Token.Type.Nullify()), right, left.Nullify());

                if (!hasNull)
                    return contains;

                return Expression.Or(Expression.Equal(left, Expression.Constant(null, Token.Type.Nullify())), contains);
            }
            else
            {
{'fraction_non_alphanumeric': 0.09789830508474576, 'fraction_numerical': 0.0002711864406779661, 'mean_word_length': 2.453183520599251, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208828', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1766, 'n_words': 381}