diff --git a/src/Tingle.Extensions.MongoDB/Extensions/IMongoQueryableExtensions.cs b/src/Tingle.Extensions.MongoDB/Extensions/IMongoQueryableExtensions.cs index ef3facd..90dca13 100644 --- a/src/Tingle.Extensions.MongoDB/Extensions/IMongoQueryableExtensions.cs +++ b/src/Tingle.Extensions.MongoDB/Extensions/IMongoQueryableExtensions.cs @@ -1,5 +1,6 @@ using MongoDB.Driver; using MongoDB.Driver.Linq; +using System.Linq.Expressions; namespace System.Linq; @@ -8,61 +9,221 @@ namespace System.Linq; /// public static class IMongoQueryableExtensions { + /// + /// Returns the number of elements in a sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// The that contains the elements to be counted. + /// The cancellation token. + /// The number of elements in the input sequence. + public static Task LongCountInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.LongCountAsync(cancellationToken); + } + + /// + /// Returns the number of elements in the specified sequence that satisfies a condition. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// The that contains the elements to be counted. + /// A function to test an element for a condition. + /// The cancellation token. + /// The number of elements in the sequence that satisfies the condition in the predicate function. + public static Task LongCountInMongoAsync(this IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.LongCountAsync(predicate, cancellationToken); + } + /// /// Returns a list containing all the documents returned from Mongo. /// This method removes the need to convert instances of to . /// - /// - /// - /// - /// - public static Task> ToListInMongoAsync(this IQueryable query, CancellationToken cancellationToken = default) + /// The type of the document. + /// An . + /// The cancellation token. + /// A Task whose value is the list of documents. + public static Task> ToListInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) { - ArgumentNullException.ThrowIfNull(query); + ArgumentNullException.ThrowIfNull(source); // cast to IMongoQueryable, if not possible, a type cast exception is thrown - var mq = (IMongoQueryable)query; + var mq = (IMongoQueryable)source; // execute the async operation return mq.ToListAsync(cancellationToken); } + /// + /// Returns the first element of a sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return the first element of. + /// The cancellation token. + /// The first element in source. + public static Task FirstInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.FirstAsync(cancellationToken); + } + + /// + /// Returns the first element of a sequence that satisfies a specified condition. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return an element from. + /// A function to test an element for a condition. + /// The cancellation token. + /// The first element in source that passes the test in predicate. + public static Task FirstInMongoAsync(this IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.FirstAsync(predicate, cancellationToken); + } + + /// + /// Returns the first element of a sequence, or a default value if the sequence contains no elements. + /// This method throws an exception if there is more than one element in the sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return the first element of. + /// The cancellation token. + /// The first element of the input sequence, or default(T) if the sequence contains no elements. + public static Task FirstOrDefaultInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.FirstOrDefaultAsync(cancellationToken); + } + + /// + /// Returns the first element of a sequence that satisfies a specified condition or a default value if no such element is found. + /// This method throws an exception if there is more than one element in the sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return a first element from. + /// A function to test an element for a condition. + /// The cancellation token. + /// The first element of the input sequence that satisfies the condition in predicate, or default(T) if no such element is found. + public static Task FirstOrDefaultInMongoAsync(this IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.FirstOrDefaultAsync(predicate, cancellationToken); + } + /// /// Returns the only element of a sequence from Mongo, and throws an exception if there is not exactly one element in the sequence. /// This method removes the need to convert instances of to . /// - /// - /// - /// - /// - public static Task SingleInMongoAsync(this IQueryable query, CancellationToken cancellationToken = default) + /// The type of the elements of source. + /// An to return the single element of. + /// The cancellation token. + /// The single element of the input sequence. + public static Task SingleInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) { - ArgumentNullException.ThrowIfNull(query); + ArgumentNullException.ThrowIfNull(source); // cast to IMongoQueryable, if not possible, a type cast exception is thrown - var mq = (IMongoQueryable)query; + var mq = (IMongoQueryable)source; // execute the async operation return mq.SingleAsync(cancellationToken); } + /// + /// Returns the only element of a sequence from Mongo, and throws an exception if there is not exactly one element in the sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return a single element from. + /// A function to test an element for a condition. + /// The cancellation token. + /// The single element of the input sequence that satisfies the condition in predicate. + public static Task SingleInMongoAsync(this IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.SingleAsync(predicate, cancellationToken); + } + /// /// Returns the only element of a sequence from Mongo, or a default value if the sequence is empty. /// This method throws an exception if there is more than one element in the sequence. /// This method removes the need to convert instances of to . /// - /// - /// - /// - /// - public static Task SingleOrDefaultInMongoAsync(this IQueryable query, CancellationToken cancellationToken = default) + /// The type of the elements of source. + /// An to return the single element of. + /// The cancellation token. + /// The single element of the input sequence, or default(T) if the sequence contains no elements. + public static Task SingleOrDefaultInMongoAsync(this IQueryable source, CancellationToken cancellationToken = default) { - ArgumentNullException.ThrowIfNull(query); + ArgumentNullException.ThrowIfNull(source); // cast to IMongoQueryable, if not possible, a type cast exception is thrown - var mq = (IMongoQueryable)query; + var mq = (IMongoQueryable)source; // execute the async operation return mq.SingleOrDefaultAsync(cancellationToken); } + + /// + /// Returns the only element of a sequence from Mongo, or a default value if the sequence is empty. + /// This method throws an exception if there is more than one element in the sequence. + /// This method removes the need to convert instances of to . + /// + /// The type of the elements of source. + /// An to return a single element from. + /// A function to test an element for a condition. + /// The cancellation token. + /// The single element of the input sequence that satisfies the condition in predicate, or default(T) if no such element is found. + public static Task SingleOrDefaultInMongoAsync(this IQueryable source, Expression> predicate, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(source); + + // cast to IMongoQueryable, if not possible, a type cast exception is thrown + var mq = (IMongoQueryable)source; + + // execute the async operation + return mq.SingleOrDefaultAsync(predicate, cancellationToken); + } }