2015-06-09 133 views
0

運行時如何使用official drivers生成過濾器?我想要得到的聯繫人包含特定的用戶ID和可以過濾一些屬性(包含一些文本)通過MongoDB的多個過濾器C#驅動程序

當前代碼不與request.Filter > 1工作:

private FilterDefinition<Contact> BuildFilter(NgTableRequest request, string userId) 
{ 
    var filters = new List<FilterDefinition<Contact>> 
    { 
     Builders<Contact>.Filter.Where(q => q.ContactUsers.Any(w => w.UserId == userId)) 
    }; 

    if (request.Filter != null && request.Filter.Any()) 
    { 
     foreach (var reqFilter in request.Filter) 
     { 
      filters.Add(Builders<Contact>.Filter.Regex(reqFilter.Key, reqFilter.Value[0])); 
     } 
    } 

    var result = Builders<Contact>.Filter.And(filters); 

    return result; 
} 

回答

4

檢查產生的過濾器使用下面的代碼,而在另一個發現錯誤放置

var documentSerializer = BsonSerializer.SerializerRegistry.GetSerializer<Contact>(); 
    var renderedFilter = result.Render(documentSerializer, BsonSerializer.SerializerRegistry).ToString(); 
    Trace.WriteLine("Filter: " + renderedFilter); 
相關問題