Has anyone been able to use successfully PredicateBuilder from albahari.com against MongoDB?

主宰稳场 提交于 2019-12-10 21:33:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!