You are currently browsing legacy 4.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to Stream Query Results

Query results can be streamed using the Stream method from the Advanced session operations. The query can be issued using either a static index, or it can be a dynamic one where it will be handled by an auto index.

Syntax

IEnumerator<StreamResult<T>> Stream<T>(IQueryable<T> query);

IEnumerator<StreamResult<T>> Stream<T>(
    IQueryable<T> query,
    out StreamQueryStatistics streamQueryStats);

IEnumerator<StreamResult<T>> Stream<T>(IDocumentQuery<T> query);

IEnumerator<StreamResult<T>> Stream<T>(
    IDocumentQuery<T> query,
    out StreamQueryStatistics streamQueryStats);

IEnumerator<StreamResult<T>> Stream<T>(IRawDocumentQuery<T> query);

IEnumerator<StreamResult<T>> Stream<T>(
    IRawDocumentQuery<T> query,
    out StreamQueryStatistics streamQueryStats);

Parameters
query IQueryable, IDocumentQuery or IRawDocumentQuery Query to stream results for.
out streamQueryStats StreamQueryStatistics Information about performed query.
Return Value
IEnumerator<StreamResult> Enumerator with entities.

Example I - Using Static Index

IQueryable<Employee> query = session
    .Query<Employee, Employees_ByFirstName>()
    .Where(x => x.FirstName == "Robert");

IEnumerator<StreamResult<Employee>> results = session.Advanced.Stream(query);

while (results.MoveNext())
{
    StreamResult<Employee> employee = results.Current;
}
IQueryable<Employee> query = asyncSession
    .Query<Employee, Employees_ByFirstName>()
    .Where(x => x.FirstName == "Robert");

IAsyncEnumerator<StreamResult<Employee>> results = await asyncSession.Advanced.StreamAsync(query);

while (await results.MoveNextAsync())
{
    StreamResult<Employee> employee = results.Current;
}

Example II - Dynamic Document Query

IDocumentQuery<Employee> query = session
    .Advanced
    .DocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert");

StreamQueryStatistics streamQueryStats;
IEnumerator<StreamResult<Employee>> results = session.Advanced.Stream(query, out streamQueryStats);

while (results.MoveNext())
{
    StreamResult<Employee> employee = results.Current;
}
IAsyncDocumentQuery<Employee> query = asyncSession
    .Advanced
    .AsyncDocumentQuery<Employee>()
    .WhereEquals(x => x.FirstName, "Robert");

IAsyncEnumerator<StreamResult<Employee>> results = await asyncSession.Advanced.StreamAsync(query);

while (await results.MoveNextAsync())
{
    StreamResult<Employee> employee = results.Current;
}

Example III - Dynamic Raw Query

IRawDocumentQuery<Employee> query = session
    .Advanced
    .RawQuery<Employee>("from Employees where FirstName = 'Robert'");

IEnumerator<StreamResult<Employee>> results = session.Advanced.Stream(query);

while (results.MoveNext())
{
    StreamResult<Employee> employee = results.Current;
}
IAsyncRawDocumentQuery<Employee> query = asyncSession
    .Advanced
    .AsyncRawQuery<Employee>("from Employees where FirstName = 'Robert'");

IAsyncEnumerator<StreamResult<Employee>> results = await asyncSession.Advanced.StreamAsync(query);

while (await results.MoveNextAsync())
{
    StreamResult<Employee> employee = results.Current;
}