3

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok. Can you give me an example of how to use it though?
@ENC0D3D it depends on what your TEntity is, but here: SearchFor(hamster => hamster.Name == "Bar")
I tried that but it returns 0 records. With and without the compile... IList<tickets> lstTickets = _ticketRepository.SearchFor(x => x.description == search);
Why wouldn't it? do you have a hamster named bar?
@ENC0D3D i don't really get what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.