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!

c-sharp linq mongodb

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Is Using LINQ in C# Bad for Performance?

LINQ (Language INtegrated Query) is a set of features in C# for concise and declarative code. I use it pretty much wherever I can, but sometimes I wonder and people ask: is this a performance trap? It would be painful to have to replace many usages of LINQ with other constructs in addition to time spent finding that to be an actual bottleneck. Let’s do a basic benchmark to see what using LINQ might cost us compared to more traditional constructs such as for and foreach loops, and see if this worry is valid.

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five

C. C++. C#. 차이점 알려드림. 5분 순삭. Explain C.C++.C#. Like I'm Five. 아시죠? C는 C++.C# 줄인말이 아니라는거?

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Learn how to perform CRUD operations using C Sharp

QuickStart: CRUD Operations in C Sharp. Learn how to perform CRUD operations using C Sharp for MongoDB databases.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.