如何使用MongoDB C#驱动程序有条件地组合过滤器?

时间:2015-06-01 06:14:00

标签: c# mongodb

考虑以下过滤器:

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 过滤器

1 个答案:

答案 0 :(得分:23)

if (criteria.CategoryId != 0)
{
    var criteriaFilter = builder.Eq(i => i.CategoryId, criteria.CategoryId);
    filter = filter & criteriaFilter;
}