using MongoDB.Driver; using MongoDB.Driver.Linq; using MongoDbGenericRepository.Models; using System; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace MongoDbGenericRepository { /// /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : ReadOnlyMongoRepository, IBaseMongoRepository { /// /// Sums the values of a selected field for a given filtered collection of documents. /// /// The type representing a Document. /// A LINQ expression filter. /// The field you want to sum. /// The partition key of your document, if any. public virtual async Task SumByAsync(Expression> filter, Expression> selector, string partitionKey = null) where TDocument : IDocument { var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); return await collection.AsQueryable() .Where(filter) .SumAsync(selector); } } }