问题
I have the following code using the popular PredicateBuilder from albahari.com:
var predicate = PredicateBuilder.False<Message>();
predicate.Or(p => p.Subject.Contains("a"));
var test = this.MongoConnectionHandler.MongoCollection.AsQueryable().Where(predicate).ToList();
return this.MongoConnectionHandler.MongoCollection.AsQueryable().Count(predicate);
The problem is that it doesn't return anything even though there are records in that column which contain the letter 'a'. Removing the predicate builder and just doing contains directly off of AsQueryable() returns the matching records.
Has anyone been able to use the PredicateBuilder library successfully with Mongo?
回答1:
I found the solution in a similar question here: https://stackoverflow.com/a/21462366/1316683
Basically add the LinqKit library and add AsExpandable to this line:
var test = this.MongoConnectionHandler.MongoCollection.AsQueryable().AsExpandable<Message>().Where(predicate).ToList();
来源:https://stackoverflow.com/questions/17759613/has-anyone-been-able-to-use-successfully-predicatebuilder-from-albahari-com-agai