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>();
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();
}
At this point, Marten's Linq support has been tested against these .Net types:
String
Int32
&Int64
(int
andlong
)Decimal
(float)DateTime
andDateTimeOffset
Enum
valuesNullable<T>
of all of the above typesBoolean
Double
Float