Compiled C# Linq expression and querying Mongo

Compiled C# Linq expression and querying Mongo

I've inherited some code from a former employee that queries a mongo DB using compiled Linq expressions and the&nbsp;<a href="https://github.com/RobThree/MongoRepository" target="_blank">MongoRepository library</a>&nbsp;(which sits on top of the MongoDB C# driver).

I've inherited some code from a former employee that queries a mongo DB using compiled Linq expressions and the MongoRepository library (which sits on top of the MongoDB C# driver).

These were taking a long time to run - usually around 6 minutes(!) and causing problems with controller methods that used them.

So I've simplified the code and removed the call to .Compile() the lambda expression and this has seemed to solve the issue (takes <10s to run now).

My questions is: why does compiling this expression cause problems when querying mongo?

This was the gist of the original code (hacked out, so out of context sorry):

public class BaseMongoRepository<T> : MongoRepository<T, Guid> where T : IEntity<Guid> {

protected BaseMongoRepository(string connectionString) : base(connectionString) { }

protected bool IsSatisfiedBy(T entity) {
    Expression&lt;Func&lt;T, bool&gt;&gt; func = x =&gt; x != null &amp;&amp; x.ToString() == "foo"; // query was passed in, but you get the idea
    var predicate = func.Compile(); // THIS LINE??
    return predicate(entity);
}

public IEnumerable&lt;T&gt; Find() {
    return base.collection.AsQueryable().Where(IsSatisfiedBy);
}

}

And I simplified it to something that just uses a regular predicate Func:

public IEnumerable<T> Find() {
    return base.collection.AsQueryable().Where(x => x != null && x.ToString() == "foo");
}

Any thoughts most appreciated!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective