Search code examples
c#.netmongodbiqueryablemongodb-.net-driver

MongoDb passing predicate argument


in MongoDb I can pass a predicate to an queryable instance for example

DataBase.GetCollection<BsonDocument>("entity")
    .AsQueryable<Entity>()
    .Where(item=>item.id ==5);

But now I have function like this

IEnumerbale QueryData(Predicate<Entity> condition)
{
    this.DataBase.GetCollection<BsonDocument>("entity")
        .AsQueryable<Entity>()
        .Where(item=> condition(item));
}

but this does not work and tells me:

Unsupported where clause: .

Is this as designed? is there any workaround ? Am I doing something wrong?


Solution

  • You're not even passing an expression. Your condition is a totally opaque function to MongoDB.

    You need to pass in an Expression<Func<Entity,bool>> and call Where like this:

    Where(condition)