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 ReadOnlyMongoRepository
{
///
/// 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
{
return await SumByAsync(filter, selector, partitionKey);
}
///
/// 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
where TKey : IEquatable
{
return await GetCollection(partitionKey)
.AsQueryable()
.Where(filter)
.SumAsync(selector);
}
}
}