Пример #1
0
        /// <summary>
        /// Combines the first expression with the second using the specified merge function.
        /// </summary>
        /// <param name="first">The first expression.</param>
        /// <param name="second">The second expression.</param>
        /// <param name="merge">The merge function.</param>
        /// <typeparam name="T">Element type.</typeparam>
        private static Expression <T> Compose <T>(
            [NotNull] this Expression <T> first,
            [NotNull] Expression <T> second,
            [NotNull] Func <Expression, Expression, Expression> merge)
        {
            var map = first.Parameters
                      .Select((f, i) => new { f, s = second.Parameters[i] })
                      .ToDictionary(p => p.s, p => (Expression)p.f);

            var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

            return(Expression.Lambda <T>(merge(first.Body, secondBody), first.Parameters));
        }
Пример #2
0
        /// <inheritdoc />
        public Expression <Func <TEntity, bool> > Where <TEntity, TInput>(
            [NotNull] Expression <Func <TEntity, TInput, bool> > predicate,
            TInput input)
        {
            if (IgnoreDefaults && Equals(input, default(TInput)))
            {
                return(null);
            }

            if (IgnoreDefaults && input is IEnumerable enumerable)
            {
                var enumerator = enumerable.GetEnumerator();
                var disposable = enumerator as IDisposable;
                try
                {
                    if (!enumerator.MoveNext())
                    {
                        return(null);
                    }
                }
                finally
                {
                    disposable?.Dispose();
                }
            }

            if (typeof(TInput) == typeof(string))
            {
                return(StringWhere(predicate as Expression <Func <TEntity, string, bool> >, input as string));
            }

            var replacement = new Dictionary <ParameterExpression, Expression>
            {
                { predicate.Parameters.Last(), Expression.Constant(input, typeof(TInput)) }
            };

            var body = ParameterRebinder.ReplaceParameters(
                replacement,
                predicate.Body);

            var filter = Expression.Lambda <Func <TEntity, bool> >(
                body,
                predicate.Parameters.First());

            return(filter);
        }
Пример #3
0
        /// <inheritdoc cref="Where{TEntity,TInput}"/>
        private Expression <Func <TEntity, bool> > StringWhere <TEntity>(
            [NotNull] Expression <Func <TEntity, string, bool> > predicate,
            string input)
        {
            if (IgnoreDefaults && string.IsNullOrWhiteSpace(input))
            {
                return(null);
            }

            var replacement = new Dictionary <ParameterExpression, Expression>
            {
                { predicate.Parameters.Last(), Expression.Constant(ToLower(input), typeof(string)) }
            };

            var body = ParameterRebinder.ReplaceParameters(
                replacement,
                predicate.Body);

            var filter = Expression.Lambda <Func <TEntity, bool> >(
                body,
                predicate.Parameters.First());

            return(filter);
        }