Question

How does one use the following Method on the awesome MongoDB c# Driver!????

public IList<TEntity>SearchFor(Expression<Func<TEntity, bool>> predicate)
            {
                return collection
                    .AsQueryable<TEntity>()
                        .Where(predicate.Compile())
                            .ToList();
            }

Examples would be ideal!

Was it helpful?

Solution

Simply remove the Compile because that creates a delegate that the driver can't translate into a mongo query:

public IList<TEntity>SearchFor(Expression<Func<TEntity, bool>> predicate)
{
    return collection
        .AsQueryable<TEntity>()
            .Where(predicate)
                .ToList();
}

It does however means that the predicate expression must be translatable by the MongoDB driver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top