考虑以下过滤器:
var builder = Builders<Product>.Filter;
var filter = builder.Gte(i => i.Price, criteria.MinPrice) &
builder.Lte(i => i.Price, criteria.MaxPrice);
if (0 != criteria.CategoryId)
//Combine the following filter with the previous filter. How??
var criteriaFilter = builder.Eq(i => i.CategoryId, criteria.CategoryId);
如何合并 criteriaFilter 和过滤器?
答案 0 :(得分:23)
if (criteria.CategoryId != 0)
{
var criteriaFilter = builder.Eq(i => i.CategoryId, criteria.CategoryId);
filter = filter & criteriaFilter;
}