-
Notifications
You must be signed in to change notification settings - Fork 0
LINQ Support
Aryeh Citron edited this page May 12, 2026
·
3 revisions
InMemoryEmulator.MongoDB supports LINQ queries via AsQueryable(). The MongoDB .NET Driver translates LINQ expressions into aggregation pipeline operations, which the emulator then evaluates.
Note: MongoDB.Driver 3.x uses LINQ3 exclusively. The LINQ2 provider was removed in the 3.0 release.
var queryable = collection.AsQueryable();
// Where
var results = queryable.Where(x => x.Status == "active").ToList();
// OrderBy
var sorted = queryable.OrderBy(x => x.Name).ToList();
// First / Single
var first = queryable.First(x => x.Id == someId);
// Count
var count = queryable.Count(x => x.IsActive);var names = queryable.Select(x => new { x.Name, x.Email }).ToList();var grouped = queryable
.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, Count = g.Count(), Total = g.Sum(x => x.Price) })
.ToList();var page = queryable.OrderBy(x => x.Name).Skip(20).Take(10).ToList();| Area | Behavior |
|---|---|
Nullable<T> comparisons |
Edge cases in null propagation may differ |
| Complex nested projections | Very deep projections may evaluate differently |
IQueryable extensions |
Custom extension methods not recognized by the driver won't translate |
-
Use
AsQueryable()for LINQ queries — the driver translates them to aggregation pipeline operations -
For complex queries, prefer the fluent builder API (
Builders<T>.Filter/.Update) over LINQ — it maps directly to MongoDB operators without translation
- Filter & Update Operators — MongoDB operator reference
- Aggregation Pipeline — Pipeline stages and expressions
Getting Started
Integration & Dependency Injection
Data Management
Reference
- Feature Comparison
- Features
- Filter & Update Operators
- Aggregation Pipeline
- LINQ Support
- API Reference
Help