示例#1
0
        /// <summary>
        /// Deletes records from source query into target table asynchronously and outputs deleted records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <param name="outputTable">Output table.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Number of affected records.</returns>
        /// <remarks>
        /// Database support:
        /// <list type="bullet">
        /// <item>SQL Server 2005+</item>
        /// </list>
        /// </remarks>
        public static Task <int> DeleteWithOutputIntoAsync <TSource, TOutput>(
            this IQueryable <TSource> source,
            ITable <TOutput> outputTable,
            Expression <Func <TSource, TOutput> > outputExpression,
            CancellationToken token = default)
            where TOutput : notnull
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var expr =
                Expression.Call(
                    null,
                    MethodHelper.GetMethodInfo(DeleteWithOutputInto, source, outputTable, outputExpression),
                    currentSource.Expression,
                    ((IQueryable <TOutput>)outputTable).Expression,
                    Expression.Quote(outputExpression));

            if (currentSource is IQueryProviderAsync queryAsync)
            {
                return(queryAsync.ExecuteAsync <int>(expr, token));
            }

            return(Task.Run(() => currentSource.Provider.Execute <int>(expr), token));
        }
        /// <summary>
        /// Inserts records from source query into target table and outputs inserted records into <paramref name="outputTable"/>.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TTarget">Target table record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for insert operation.</param>
        /// <param name="target">Target table.</param>
        /// <param name="setter">Inserted record constructor expression.
        /// Expression supports only target table record new expression with field initializers.</param>
        /// <param name="outputTable">Output table.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <returns>Number of affected records.</returns>
        public static int InsertWithOutputInto <TSource, TTarget, TOutput>(
            this IQueryable <TSource> source,
            ITable <TTarget> target,
            [InstantHandle] Expression <Func <TSource, TTarget> > setter,
            ITable <TOutput> outputTable,
            Expression <Func <TTarget, TOutput> > outputExpression)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (setter == null)
            {
                throw new ArgumentNullException(nameof(setter));
            }
            if (outputTable == null)
            {
                throw new ArgumentNullException(nameof(outputTable));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(source.Provider.Execute <int>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(InsertWithOutputInto, source, target, setter, outputTable, outputExpression),
                           currentSource.Expression, ((IQueryable <TTarget>)target).Expression, Expression.Quote(setter),
                           ((IQueryable <TTarget>)outputTable).Expression, Expression.Quote(outputExpression))));
        }
示例#3
0
        /// <summary>
        /// Deletes records from source query into target table asynchronously and returns deleted records.
        /// </summary>
        /// <typeparam name="TSource">Source query record type.</typeparam>
        /// <typeparam name="TOutput">Output table record type.</typeparam>
        /// <param name="source">Source query, that returns data for delete operation.</param>
        /// <param name="outputExpression">Output record constructor expression.
        /// Expression supports only record new expression with field initializers.</param>
        /// <param name="token">Optional asynchronous operation cancellation token.</param>
        /// <returns>Array of records.</returns>
        public static Task <TOutput[]> DeleteWithOutputAsync <TSource, TOutput>(
            this IQueryable <TSource> source,
            Expression <Func <TSource, TOutput> > outputExpression,
            CancellationToken token = default)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (outputExpression == null)
            {
                throw new ArgumentNullException(nameof(outputExpression));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            return(currentSource.Provider.CreateQuery <TOutput>(
                       Expression.Call(
                           null,
                           MethodHelper.GetMethodInfo(DeleteWithOutput, source, outputExpression),
                           currentSource.Expression,
                           Expression.Quote(outputExpression)))
                   .ToArrayAsync(token));
        }
示例#4
0
        [Pure]         // Methods.LinqToDB.ThenLoadFromManyManyFilter
        public static ILoadWithQueryable <TEntity, TProperty> ThenLoad <TEntity, TPreviousProperty, TProperty>(
            this ILoadWithQueryable <TEntity, IEnumerable <TPreviousProperty> > source,
            [InstantHandle] Expression <Func <TPreviousProperty, IEnumerable <TProperty> > > selector,
            [InstantHandle] Expression <Func <IQueryable <TProperty>, IQueryable <TProperty> > > loadFunc)
            where TEntity : class
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            var currentSource = ProcessSourceQueryable?.Invoke(source) ?? source;

            var result = currentSource.Provider.CreateQuery <TEntity>(
                Expression.Call(null,
                                MethodHelper.GetMethodInfo(ThenLoad, source, selector, loadFunc),
                                currentSource.Expression, Expression.Quote(selector), Expression.Quote(loadFunc)));

            return(new LoadWithQueryable <TEntity, TProperty>(result));
        }