diff --git a/MongoDbGenericRepository/BaseMongoDbRepository.Maths.cs b/MongoDbGenericRepository/BaseMongoDbRepository.Maths.cs new file mode 100644 index 0000000..bbc6552 --- /dev/null +++ b/MongoDbGenericRepository/BaseMongoDbRepository.Maths.cs @@ -0,0 +1,37 @@ +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); + } + + } +} \ No newline at end of file diff --git a/MongoDbGenericRepository/BaseMongoDbRepository.cs b/MongoDbGenericRepository/BaseMongoDbRepository.cs index 7a3a76e..2a3f07e 100644 --- a/MongoDbGenericRepository/BaseMongoDbRepository.cs +++ b/MongoDbGenericRepository/BaseMongoDbRepository.cs @@ -822,7 +822,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) @@ -840,7 +840,7 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task ProjectOneAsync(Expression> filter, Expression> projection, string partitionKey = null) @@ -849,8 +849,8 @@ namespace MongoDbGenericRepository where TProjection : class { return await HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .FirstOrDefaultAsync(); + .Project(projection) + .FirstOrDefaultAsync(); } /// @@ -858,7 +858,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) @@ -876,7 +876,7 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual TProjection ProjectOne(Expression> filter, Expression> projection, string partitionKey = null) @@ -885,8 +885,8 @@ namespace MongoDbGenericRepository where TProjection : class { return HandlePartitioned(partitionKey).Find(filter) - .Project(projection) - .FirstOrDefault(); + .Project(projection) + .FirstOrDefault(); } /// @@ -894,7 +894,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) @@ -912,7 +912,7 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the primary key for a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual async Task> ProjectManyAsync(Expression> filter, Expression> projection, string partitionKey = null) @@ -930,7 +930,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type representing the model you want to project to. - /// + /// A LINQ expression filter. /// The projection expression. /// An optional partition key. public virtual List ProjectMany(Expression> filter, Expression> projection, string partitionKey = null) @@ -996,7 +996,7 @@ namespace MongoDbGenericRepository /// The type representing a Document. /// The type of the grouping criteria. /// The type of the projected group. - /// + /// A LINQ expression filter. /// The grouping criteria. /// The projected group result. /// The partition key of your document, if any. @@ -1021,7 +1021,7 @@ namespace MongoDbGenericRepository /// Asynchronously returns a paginated list of the documents matching the filter condition. /// /// The type representing a Document. - /// + /// A LINQ expression filter. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. @@ -1036,7 +1036,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type of the primary key for a Document. - /// + /// A LINQ expression filter. /// The number of documents you want to skip. Default value is 0. /// The number of documents you want to take. Default value is 50. /// An optional partition key. @@ -1053,7 +1053,7 @@ namespace MongoDbGenericRepository /// GetAndUpdateOne with filter /// /// The type representing a Document. - /// + /// A LINQ expression filter. /// /// /// @@ -1067,7 +1067,7 @@ namespace MongoDbGenericRepository /// /// The type representing a Document. /// The type of the primary key for a Document. - /// + /// A LINQ expression filter. /// /// ///