使用C#mongo驱动程序构建查询

时间:2016-03-20 14:22:43

标签: c# mongodb mongodb-.net-driver

我想知道如何使用mongo驱动程序构建复杂查询。这是我的问题:

  view_splash(0)
NameError: name 'view_splash' is not defined

使用var builder = Builders<MongoNavFilter>.Filter; var query = builder.Where(x => x.Link == link && x.SubLink == subLink); if (some statement) { var finalExpression = ... query = query & Builders<MongoNavFilter>.Filter.Where(finalExpression); } if (onsale) query = query & Builders<MongoNavFilter>.Filter.Where(x => !(x.Promo == null && x.Promo == string.Empty)); var filters = _db.GetCollection<MongoNavFilter>("NavFilters"). Find(query).ToList(); EF此代码工作正常,但我无法使用IQueryable构建正确的查询。 谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:1)

知道finalExpression会有什么帮助,以防出现问题。在我看来,mongodb驱动程序翻译!(x.Promo == null && x.Promo == string.Empty)

可能存在问题

您可以尝试以下代码而不是您的代码,看看它是否会产生影响

var builder = Builders<MongoNavFilter>.Filter;
var query = builder.Eq(x => x.Link, link) & builder.Eq(x=> x.SubLink, subLink);
if (some statement)
{
    var finalExpression = ... // write this using the same syntax 
    query = query & filter; 
}
if (onsale)
    query = query & builder.Ne(x=>x.Promo, null) & builder.Ne(x=> x. Promo, string.Empty);

var filters = _db.GetCollection<MongoNavFilter>("NavFilters").
                Find(query).ToList();