see on GitHub

Session : Querying : How to perform queries lazily?

In some situations query execution must be delayed. To cover such scenario Lazily and many others query extensions has been introduced.

Lazily and LazilyAsync

Lazy<IEnumerable<T>> Lazily<T>(
	this IQueryable<T> source) { ... }

Lazy<IEnumerable<T>> Lazily<T>(
	this IQueryable<T> source,
	Action<IEnumerable<T>> onEval) { ... }

Lazy<Task<IEnumerable<T>>> LazilyAsync<T>(
	this IQueryable<T> source) { ... }

Lazy<Task<IEnumerable<T>>> LazilyAsync<T>(
	this IQueryable<T> source,
	Action<IEnumerable<T>> onEval) { ... }

Parameters
onEval Action<IEnumerable<TResult>> Action that will be performed on query results.
Return Value
Lazy<IEnumerable<TResult>> Lazy query initializer returning query results.

Example

Lazy<IEnumerable<Employee>> employeesLazy = session
	.Query<Employee>()
	.Where(x => x.FirstName == "Robert")
	.Lazily();

IEnumerable<Employee> employees = employeesLazy.Value; // query will be executed here

or

Lazy<Task<IEnumerable<Employee>>> employeesLazy = session
	.Query<Employee>()
	.Where(x => x.FirstName == "Robert")
	.LazilyAsync();

IEnumerable<Employee> employees = await employeesLazy.Value; // query will be executed here

CountLazily

Lazy<int> CountLazily<T>(
	this IRavenQueryable<T> source) { ... }

Return Value
Lazy<IEnumerable<TResult>> Lazy query initializer returning count of matched documents.

Example

Lazy<int> countLazy = session
	.Query<Employee>()
	.Where(x => x.FirstName == "Robert")
	.CountLazily();

int count = countLazy.Value; // query will be executed here

SuggestLazy

Lazy<SuggestionQueryResult> SuggestLazy(
	this IQueryable queryable) { ... }

Lazy<SuggestionQueryResult> SuggestLazy(
	this IQueryable queryable,
	SuggestionQuery query) { ... }

Parameters
query SuggestionQuery A suggestion query definition containing all information required to query a specified index.
Return Value
Lazy<SuggestionQueryResult> Lazy query initializer containing array of all suggestions for executed query.

Example

Lazy<SuggestionQueryResult> suggestLazy = session
	.Query<Employee>()
	.SuggestLazy();

SuggestionQueryResult suggest = suggestLazy.Value; // query will be executed here
string[] suggestions = suggest.Suggestions;

ToFacetsLazy

Lazy<FacetResults> ToFacetsLazy<T>(
	this IQueryable<T> queryable,
	string facetSetupDoc,
	int start = 0,
	int? pageSize = null) { ... }

Lazy<FacetResults> ToFacetsLazy<T>(
	this IQueryable<T> queryable,
	IEnumerable<Facet> facets,
	int start = 0,
	int? pageSize = null) { ... }

Parameters
facets List<Facet> List of facets required to perform a facet query (mutually exclusive with facetSetupDoc).
facetSetupDoc string Document key that contains predefined FacetSetup (mutually exclusive with facets).
start int number of results that should be skipped. Default: 0.
pageSize int maximum number of results that will be retrieved. Default: null.
Return Value
Lazy<FacetResults> Lazy query initializer containing Facet query results with query Duration and list of Results - one entry for each term/range as specified in [FacetSetup] document or passed in parameters.

Example

Lazy<FacetResults> facetsLazy = session
	.Query<Camera>("Camera/Costs")
	.ToFacetsLazy("facets/CameraFacets");

FacetResults facets = facetsLazy.Value; // query will be executed here
Dictionary<string, FacetResult> results = facets.Results;