Exemplo n.º 1
0
        /// <summary>
        /// Creates a project many query against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <typeparam name="TProjection">The type of the projection.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new project many query.</returns>
        public static ProjectManyQueryAsync <TModel, TMetadata, TProjection> ProjectManyAsync <TModel, TMetadata, TProjection>(
            MongoDatabase database)
        {
            var projectManyQueryAsync = ProjectManyAsync <StorageModel <TModel, TMetadata>, TProjection>(database);

            ProjectManyQueryAsync <TModel, TMetadata, TProjection> queryAsync = async(where, project, collectionName, cancellationToken) =>
            {
                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                return(await projectManyQueryAsync(where, project, modelTypeName, cancellationToken));
            };

            return(queryAsync);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a project many query against the specified database.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TProjection">The type of the projection.</typeparam>
        /// <param name="database">The database.</param>
        /// <returns>A new project many query.</returns>
        public static ProjectManyQueryAsync <TModel, TProjection> ProjectManyAsync <TModel, TProjection>(MongoDatabase database)
        {
            if (database == null)
            {
                throw new ArgumentNullException(nameof(database));
            }

            ProjectManyQueryAsync <TModel, TProjection> queryAsync = async(where, project, collectionName, cancellationToken) =>
            {
                if (where == null)
                {
                    throw new ArgumentNullException((nameof(where)));
                }

                if (project == null)
                {
                    throw new ArgumentNullException((nameof(project)));
                }

                var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

                var client     = database.CreateClient();
                var collection = client.GetCollection <TModel>(modelTypeName);

                var projectionDefinition = Builders <TModel> .Projection.Expression(project);

                var findOptions = new FindOptions <TModel, TProjection>()
                {
                    Projection = projectionDefinition
                };

                var findResults = await collection.FindAsync(where, findOptions, cancellationToken);

                return(await findResults.ToListAsync(cancellationToken));
            };

            return(queryAsync);
        }