Skip to main content

Session: Querying: How to Perform Queries Lazily

In some situations, query execution must be delayed. To cover such a scenario, Lazily and many other query extensions have been introduced.

Lazily and LazilyAsync

// 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
Parameters
onEvalAction<IEnumerable<TResult>>An action that will be performed on the 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

Counts

// 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
Return Value
Lazy<int>Lazy query initializer returning a count of matched documents.

Example

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

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

Suggestions

// Define a lazy DocumentQuery
Lazy<int> lazyCount = session.Advanced
.DocumentQuery<Employee>()
.WhereEquals(x => x.FirstName, "Robert")
// Add a call to 'CountLazily'
.CountLazily();

int count = lazyCount.Value; // DocumentQuery is executed here
Return Value
Lazy<Dictionary<string, SuggestionResult>>Lazy query initializer containing a dictionary with suggestions for matching executed query

Example

// 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;

Facets

// Define a lazy suggestion query
Lazy<Task<Dictionary<string, SuggestionResult>>> lazySuggestions = asyncSession
.Query<Employee>()
.SuggestUsing(builder => builder.ByField("Name", "chaig"))
// Add a call to 'ExecuteLazyAsync'
.ExecuteLazyAsync();

Dictionary<string, SuggestionResult> suggest = await lazySuggestions.Value; // Query is executed here
List<string> suggestions = suggest["Name"].Suggestions;
Return Value
Lazy<Dictionary<string, FacetResult>>Lazy query initializer containing a dictionary with facet results matching executed query

Example

// Define a lazy DocumentQuery
Lazy<Dictionary<string, SuggestionResult>> lazySuggestions = session.Advanced
.DocumentQuery<Employee>()
.SuggestUsing(builder => builder.ByField("Name", "chaig"))
// Add a call to 'ExecuteLazy'
.ExecuteLazy();

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