<commit_before>﻿//Copyright (C) Microsoft Corporation.  All rights reserved.
//Thank you Microsoft!

using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using DynamicQueryable;

namespace System.Linq.Dynamic {

    public static class DynamicQueryable {

        public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)Where((IQueryable)source, predicate, values);
        }

        public static IQueryable Where(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Where",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable Select(this IQueryable source, string selector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (selector == null) throw new ArgumentNullException("selector");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Select",
                    new Type[] { source.ElementType, lambda.Body.Type },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable SelectMany(this IQueryable source, string selector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (selector == null) throw new ArgumentNullException("selector");

            // Parse the lambda
            LambdaExpression lambda =
                DynamicExpression.ParseLambda(source.ElementType, null, selector, values);

            // Fix lambda by recreating to be of correct Func<> type in case 
            // the expression parsed to something other than IEnumerable<T>.
            // For instance, a expression evaluating to List<T> would result 
            // in a lambda of type Func<T, List<T>> when we need one of type
            // an Func<T, IEnumerable<T> in order to call SelectMany().
            Type inputType = source.Expression.Type.GetGenericArguments()[0];
            Type resultType = lambda.Body.Type.GetGenericArguments()[0];
            Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
            Type delegateType = typeof(Func<,>).MakeGenericType(inputType, enumerableType);
            lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);

            // Create the new query
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "SelectMany",
                    new Type[] { source.ElementType, resultType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) {
            return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values);
        }

        public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (ordering == null) throw new ArgumentNullException("ordering");
            ParameterExpression[] parameters = new ParameterExpression[] {
                Expression.Parameter(source.ElementType, "") };
            ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
            IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
            Expression queryExpr = source.Expression;
            string methodAsc;
            string methodDesc;
#if NET_STANDARD
            if (queryExpr.Type.GetTypeInfo().IsGenericType && queryExpr.Type.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>)) {
#else
            if (queryExpr.Type.IsGenericType && queryExpr.Type.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>)) {
#endif
                methodAsc = "ThenBy";
                methodDesc = "ThenByDescending";
            }
            else {
                methodAsc = "OrderBy";
                methodDesc = "OrderByDescending";
            }
            foreach (DynamicOrdering o in orderings) {
                queryExpr = Expression.Call(
                    typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
                    new Type[] { source.ElementType, o.Selector.Type },
                    queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
                methodAsc = "ThenBy";
                methodDesc = "ThenByDescending";
            }
            return source.Provider.CreateQuery(queryExpr);
        }

        public static IQueryable Take(this IQueryable source, int count) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Take",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Constant(count)));
        }

        public static IQueryable Skip(this IQueryable source, int count) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Skip",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Constant(count)));
        }

        public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (keySelector == null) throw new ArgumentNullException("keySelector");
            if (elementSelector == null) throw new ArgumentNullException("elementSelector");
            LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values);
            LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "GroupBy",
                    new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
                    source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));
        }

        public static IQueryable<T> SkipWhile<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)SkipWhile((IQueryable)source, predicate, values);
        }

        public static IQueryable SkipWhile(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "SkipWhile",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable<T> TakeWhile<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)TakeWhile((IQueryable)source, predicate, values);
        }

        public static IQueryable TakeWhile(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "TakeWhile",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable Join(this IQueryable source1, string alias1, IQueryable source2, string alias2, string key1, string key2, string selector, params object[] args) {
            if (source1 == null) throw new ArgumentNullException("source1");
            if (alias1 == null) throw new ArgumentNullException("alias1");
            if (source2 == null) throw new ArgumentNullException("source1");
            if (alias2 == null) throw new ArgumentNullException("alias2");
            if (key1 == null) throw new ArgumentNullException("key1");
            if (key2 == null) throw new ArgumentNullException("key2");
            if (selector == null) throw new ArgumentNullException("selector");
            ParameterExpression p1 = Expression.Parameter(source1.ElementType, alias1);
            ParameterExpression p2 = Expression.Parameter(source2.ElementType, alias2);
            LambdaExpression keyLambda1 = DynamicExpression.ParseLambda(new ParameterExpression[] { p1 }, null, key1, null);
            LambdaExpression keyLambda2 = DynamicExpression.ParseLambda(new ParameterExpression[] { p2 }, null, key2, null);
            FixLambdaReturnTypes(ref keyLambda1, ref keyLambda2);
            LambdaExpression lambda = DynamicExpression.ParseLambda(new ParameterExpression[] { p1, p2 }, null, selector, args);
            return source1.Provider.CreateQuery(
              Expression.Call(
                typeof(Queryable), "Join",
                new Type[] { source1.ElementType, source2.ElementType, keyLambda1.Body.Type, lambda.Body.Type },
               source1.Expression, source2.Expression, Expression.Quote(keyLambda1), Expression.Quote(keyLambda2), Expression.Quote(lambda)
               ));
        }

        public static bool Any(this IQueryable source) {
            if (source == null) throw new ArgumentNullException("source");
            return (bool)source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "Any",
                    new Type[] { source.ElementType }, source.Expression));
        }

        public static bool All(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            var lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return (bool)source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "All",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static int Count(this IQueryable source) {
            if (source == null) throw new ArgumentNullException("source");
            return (int)source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "Count",
                    new Type[] { source.ElementType }, source.Expression));
        }

        private static void FixLambdaReturnTypes(ref LambdaExpression lambda1, ref LambdaExpression lambda2) {
            var type1 = lambda1.Body.Type;
            var type2 = lambda2.Body.Type;
            if (type1 == type2) return;

#if NET_STANDARD
            if (type2.GetTypeInfo().IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable<>) && type1 == type2.GetGenericArguments()[0])
#else
            if (type2.IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable<>) && type1 == type2.GetGenericArguments()[0])
#endif
                lambda1 = Expression.Lambda(Expression.Convert(lambda1.Body, type2), lambda1.Parameters.ToArray());
            else // this may fail because types are incompatible
                lambda2 = Expression.Lambda(Expression.Convert(lambda2.Body, type1), lambda2.Parameters.ToArray());
        }
    }

    public abstract class DynamicClass {<commit_msg>Fixing .net core bug<commit_after>﻿//Copyright (C) Microsoft Corporation.  All rights reserved.
//Thank you Microsoft!

using System.Collections.Generic;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using DynamicQueryable;

namespace System.Linq.Dynamic {

    public static class DynamicQueryable {

        public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)Where((IQueryable)source, predicate, values);
        }

        public static IQueryable Where(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Where",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable Select(this IQueryable source, string selector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (selector == null) throw new ArgumentNullException("selector");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Select",
                    new Type[] { source.ElementType, lambda.Body.Type },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable SelectMany(this IQueryable source, string selector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (selector == null) throw new ArgumentNullException("selector");

            // Parse the lambda
            LambdaExpression lambda =
                DynamicExpression.ParseLambda(source.ElementType, null, selector, values);

            // Fix lambda by recreating to be of correct Func<> type in case 
            // the expression parsed to something other than IEnumerable<T>.
            // For instance, a expression evaluating to List<T> would result 
            // in a lambda of type Func<T, List<T>> when we need one of type
            // an Func<T, IEnumerable<T> in order to call SelectMany().
            Type inputType = source.Expression.Type.GetGenericArguments()[0];
            Type resultType = lambda.Body.Type.GetGenericArguments()[0];
            Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
            Type delegateType = typeof(Func<,>).MakeGenericType(inputType, enumerableType);
            lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);

            // Create the new query
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "SelectMany",
                    new Type[] { source.ElementType, resultType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) {
            return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values);
        }

        public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (ordering == null) throw new ArgumentNullException("ordering");
            ParameterExpression[] parameters = new ParameterExpression[] {
                Expression.Parameter(source.ElementType, "") };
            ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
            IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
            Expression queryExpr = source.Expression;
            string methodAsc;
            string methodDesc;
#if NET_STANDARD
            if (queryExpr.Type.GetTypeInfo().IsGenericType && queryExpr.Type.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>)) {
#else
            if (queryExpr.Type.IsGenericType && queryExpr.Type.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>)) {
#endif
                methodAsc = "ThenBy";
                methodDesc = "ThenByDescending";
            }
            else {
                methodAsc = "OrderBy";
                methodDesc = "OrderByDescending";
            }
            foreach (DynamicOrdering o in orderings) {
                queryExpr = Expression.Call(
                    typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
                    new Type[] { source.ElementType, o.Selector.Type },
                    queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
                methodAsc = "ThenBy";
                methodDesc = "ThenByDescending";
            }
            return source.Provider.CreateQuery(queryExpr);
        }

        public static IQueryable Take(this IQueryable source, int count) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Take",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Constant(count)));
        }

        public static IQueryable Skip(this IQueryable source, int count) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "Skip",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Constant(count)));
        }

        public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (keySelector == null) throw new ArgumentNullException("keySelector");
            if (elementSelector == null) throw new ArgumentNullException("elementSelector");
            LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values);
            LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "GroupBy",
                    new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
                    source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));
        }

        public static IQueryable<T> SkipWhile<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)SkipWhile((IQueryable)source, predicate, values);
        }

        public static IQueryable SkipWhile(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "SkipWhile",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable<T> TakeWhile<T>(this IQueryable<T> source, string predicate, params object[] values) {
            return (IQueryable<T>)TakeWhile((IQueryable)source, predicate, values);
        }

        public static IQueryable TakeWhile(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            if (predicate == null) throw new ArgumentNullException("predicate");
            LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.CreateQuery(
                Expression.Call(
                    typeof(Queryable), "TakeWhile",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static IQueryable Join(this IQueryable source1, string alias1, IQueryable source2, string alias2, string key1, string key2, string selector, params object[] args) {
            if (source1 == null) throw new ArgumentNullException("source1");
            if (alias1 == null) throw new ArgumentNullException("alias1");
            if (source2 == null) throw new ArgumentNullException("source1");
            if (alias2 == null) throw new ArgumentNullException("alias2");
            if (key1 == null) throw new ArgumentNullException("key1");
            if (key2 == null) throw new ArgumentNullException("key2");
            if (selector == null) throw new ArgumentNullException("selector");
            ParameterExpression p1 = Expression.Parameter(source1.ElementType, alias1);
            ParameterExpression p2 = Expression.Parameter(source2.ElementType, alias2);
            LambdaExpression keyLambda1 = DynamicExpression.ParseLambda(new ParameterExpression[] { p1 }, null, key1, null);
            LambdaExpression keyLambda2 = DynamicExpression.ParseLambda(new ParameterExpression[] { p2 }, null, key2, null);
            FixLambdaReturnTypes(ref keyLambda1, ref keyLambda2);
            LambdaExpression lambda = DynamicExpression.ParseLambda(new ParameterExpression[] { p1, p2 }, null, selector, args);
            return source1.Provider.CreateQuery(
              Expression.Call(
                typeof(Queryable), "Join",
                new Type[] { source1.ElementType, source2.ElementType, keyLambda1.Body.Type, lambda.Body.Type },
               source1.Expression, source2.Expression, Expression.Quote(keyLambda1), Expression.Quote(keyLambda2), Expression.Quote(lambda)
               ));
        }

        public static bool Any(this IQueryable source) {
            if (source == null) throw new ArgumentNullException("source");
            return source.Provider.Execute<bool>(
                Expression.Call(
                    typeof(Queryable), "Any",
                    new Type[] { source.ElementType }, source.Expression));
        }

        public static bool All(this IQueryable source, string predicate, params object[] values) {
            if (source == null) throw new ArgumentNullException("source");
            var lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
            return source.Provider.Execute<bool>(
                Expression.Call(
                    typeof(Queryable), "All",
                    new Type[] { source.ElementType },
                    source.Expression, Expression.Quote(lambda)));
        }

        public static int Count(this IQueryable source) {
            if (source == null) throw new ArgumentNullException("source");
            return (int)source.Provider.Execute(
                Expression.Call(
                    typeof(Queryable), "Count",
                    new Type[] { source.ElementType }, source.Expression));
        }

        private static void FixLambdaReturnTypes(ref LambdaExpression lambda1, ref LambdaExpression lambda2) {
            var type1 = lambda1.Body.Type;
            var type2 = lambda2.Body.Type;
            if (type1 == type2) return;

#if NET_STANDARD
            if (type2.GetTypeInfo().IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable<>) && type1 == type2.GetGenericArguments()[0])
#else
            if (type2.IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable<>) && type1 == type2.GetGenericArguments()[0])
#endif
                lambda1 = Expression.Lambda(Expression.Convert(lambda1.Body, type2), lambda1.Parameters.ToArray());
            else // this may fail because types are incompatible
                lambda2 = Expression.Lambda(Expression.Convert(lambda2.Body, type1), lambda2.Parameters.ToArray());
        }
    }

    public abstract class DynamicClass {
{'fraction_non_alphanumeric': 0.09871076452839062, 'fraction_numerical': 0.005486108389827188, 'mean_word_length': 2.4792092706203137, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 63, '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': '4536484', 'n_tokens_mistral': 6102, 'n_tokens_neox': 5817, 'n_words': 1599}