コード例 #1
0
        /// <summary>
        /// Gets a single result.
        /// </summary>
        /// <typeparam name="TDocument">The type of the document.</typeparam>
        /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
        /// <param name="find">The fluent find.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A Task whose result is the single result.</returns>
        public static TProjection Single <TDocument, TProjection>(this IFindFluent <TDocument, TProjection> find, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(find, nameof(find));

            if (!find.Options.Limit.HasValue || find.Options.Limit.Value > 2)
            {
                find = find.Limit(2);
            }
            return(IAsyncCursorSourceExtensions.Single(find, cancellationToken));
        }
コード例 #2
0
        /// <summary>
        /// Returns the only document of the aggregate result. Throws an exception if the result set does not contain exactly one document.
        /// </summary>
        /// <typeparam name="TResult">The type of the result.</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// The fluent aggregate interface.
        /// </returns>
        public static TResult Single <TResult>(this IAggregateFluent <TResult> aggregate, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(aggregate, nameof(aggregate));

            return(IAsyncCursorSourceExtensions.Single(aggregate.Limit(2), cancellationToken));
        }