Skip to content

LINQ Support

Aryeh Citron edited this page May 12, 2026 · 3 revisions

LINQ Support

Overview

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.

Supported Operations

Basic Queries

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);

Projections

var names = queryable.Select(x => new { x.Name, x.Email }).ToList();

Aggregation

var grouped = queryable
    .GroupBy(x => x.Category)
    .Select(g => new { Category = g.Key, Count = g.Count(), Total = g.Sum(x => x.Price) })
    .ToList();

Skip / Take

var page = queryable.OrderBy(x => x.Name).Skip(20).Take(10).ToList();

Known Differences

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

Tips

  1. Use AsQueryable() for LINQ queries — the driver translates them to aggregation pipeline operations
  2. For complex queries, prefer the fluent builder API (Builders<T>.Filter / .Update) over LINQ — it maps directly to MongoDB operators without translation

See Also

Clone this wiki locally