如何使用MongoDB c#驱动程序有条件地组合过滤器
本文关键字:组合 过滤器 有条件 驱动程序 何使用 MongoDB | 更新日期: 2023-09-27 17:51:23
考虑以下过滤器:
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和filter?
if (criteria.CategoryId != 0)
{
var criteriaFilter = builder.Eq(i => i.CategoryId, criteria.CategoryId);
filter = filter & criteriaFilter;
}