Querying Documents with Linq

Marten uses the Relinq library to support a subset of the normal Linq operators as well as some Marten specific operators. Linq queries are done with Marten using the IQuerySession.Query<T>() or IDocumentSession.Query<T>() method to return an IMartenQueryable object which is in turn implements the traditional IQueryable for the document type T.

/// <summary>
/// Use Linq operators to query the documents
/// stored in Postgresql
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
IMartenQueryable<T> Query<T>();

snippet source | anchor

To query for all documents of a type - not that you would do this very often outside of testing - use the Query<T>() method like this:

public async Task get_all_documents_of_a_type(IDocumentSession session)
    // Calling ToArray() just forces the query to be executed
    var targets = await session.Query<Target>().ToListAsync();

snippet source | anchor

At this point, Marten's Linq support has been tested against these .Net types:

  1. String
  2. Int32 & Int64 (int and long)
  3. Decimal (float)
  4. DateTime and DateTimeOffset
  5. Enum values
  6. Nullable<T> of all of the above types
  7. Boolean
  8. Double
  9. Float