Skip to main content

Perform a Lazy Query

Lazy query

// Define a lazy query
Lazy<IEnumerable<Employee>> lazyEmployees = session
.Query<Employee>()
.Where(x => x.FirstName == "Robert")
// Add a call to 'Lazily'
.Lazily();

IEnumerable<Employee> employees = lazyEmployees.Value; // Query is executed here

Lazy count query

// Define a lazy count query
Lazy<int> lazyCount = session
.Query<Employee>()
.Where(x => x.FirstName == "Robert")
// Add a call to 'CountLazily'
.CountLazily();

int count = lazyCount.Value; // Query is executed here

Lazy suggestions query

// Define a lazy suggestion query
Lazy<Dictionary<string, SuggestionResult>> lazySuggestions = session
.Query<Product>()
.SuggestUsing(builder => builder.ByField(x => x.Name, "chaig"))
// Add a call to 'ExecuteLazy'
.ExecuteLazy();

Dictionary<string, SuggestionResult> suggest = lazySuggestions.Value; // Query is executed here
List<string> suggestions = suggest["Name"].Suggestions;

Lazy facets query

// Define a lazy facets query
Lazy<Dictionary<string, FacetResult>> lazyFacets = session
.Query<Product, Products_ByCategoryAndPrice>()
.AggregateBy(facetsDefinition)
// Add a call to 'ExecuteLazy'
.ExecuteLazy();

Dictionary<string, FacetResult> facets = lazyFacets.Value; // Query is executed here

FacetResult categoryResults = facets["Product Category"];
FacetResult priceResults = facets["Price per Unit"];

Syntax

// Lazy query overloads:
Lazy<IEnumerable<T>> Lazily<T>();
Lazy<IEnumerable<T>> Lazily<T>(Action<IEnumerable<T>> onEval);

Lazy<Task<IEnumerable<T>>> LazilyAsync<T>();
Lazy<Task<IEnumerable<T>>> LazilyAsync<T>(Action<IEnumerable<T>> onEval);
// Lazy count query overloads:
Lazy<int> CountLazily<T>();
Lazy<long> LongCountLazily<T>();

Lazy<Task<int>> CountLazilyAsync<T>(CancellationToken token = default(CancellationToken));
Lazy<Task<long>> LongCountLazilyAsync<T>(CancellationToken token = default(CancellationToken));
// Lazy suggestions query overloads:
Lazy<Dictionary<string, SuggestionResult>>
ExecuteLazy(Action<Dictionary<string, SuggestionResult>> onEval = null);

Lazy<Task<Dictionary<string, SuggestionResult>>>
ExecuteLazyAsync(Action<Dictionary<string, SuggestionResult>> onEval = null,
CancellationToken token = default);
// Lazy facets query overloads:
Lazy<Dictionary<string, FacetResult>>
ExecuteLazy(Action<Dictionary<string, FacetResult>> onEval = null);

Lazy<Task<Dictionary<string, FacetResult>>>
ExecuteLazyAsync(Action<Dictionary<string, FacetResult>> onEval = null,
CancellationToken token = default);
ParametersTypeDescription
onEvalAction<IEnumerable<TResult>>
Action<Dictionary<string, SuggestionResult>>
Action<Dictionary<string, FacetResult>>
An action that will be performed on the query results
when the query is executed.
Return Value
Lazy<IEnumerable<TResult>>
Lazy<int>
Lazy<Dictionary<string, SuggestionResult>>
Lazy<Dictionary<string, FacetResult>>
A lazy instance that will evaluate the query only when needed.