Session: Querying: How to query?
Database can be queried using LINQ-enabled Query
method with few custom extension that will be described later.
Syntax
IRavenQueryable<T> Query<T>(string indexName = null, string collectionName = null,
bool isMapReduce = false);
IRavenQueryable<T> Query<T, TIndexCreator>()
where TIndexCreator : AbstractIndexCreationTask, new();
Parameters | ||
---|---|---|
indexName | string | Name of an index to perform query on |
isMapReduce | bool | Indicates if queried index is a map/reduce index (modifies how we treat identifier properties). |
Return Value | |
---|---|
IRavenQueryable | Instance implementing IRavenQueryable interface containing additional query methods and extensions. |
The default value of a page size for a query is 128
results. In order to retrieve a different number of results in a single query use .Take(pageSize)
method.
Example I - Basic
// This is a Full Collection Query
// No auto-index is created since no filtering is applied
List<Employee> allEmployees = session
.Query<Employee>() // Query for all documents from 'Employees' collection
.ToList(); // Execute the query
// All 'Employee' entities are loaded and will be tracked by the session
Notice that by specifying Employee
as a type parameter, we are not only defining a result type, but also marking name of collection that will be queried.
Example II - Syntax
Queries can be performed using both LINQ syntaxes.
// This is a Full Collection Query
// No auto-index is created since no filtering is applied
List<Employee> allEmployees = await asyncSession
.Query<Employee>() // Query for all documents from 'Employees' collection
.ToListAsync(); // Execute the query
// All 'Employee' entities are loaded and will be tracked by the session
// This is a Full Collection Query
// No auto-index is created since no filtering is applied
// Query for all documents from 'Employees' collection
IRavenQueryable<Employee> query = from employee in session.Query<Employee>()
select employee;
// Execute the query
List<Employee> allEmployees = query.ToList();
// All 'Employee' entities are loaded and will be tracked by the session
Example III - Querying specified index
// This is a Full Collection Query
// No auto-index is created since no filtering is applied
// Query for all documents from 'Employees' collection
IRavenQueryable<Employee> query = from employee in asyncSession.Query<Employee>()
select employee;
// Execute the query
List<Employee> allEmployees = await query.ToListAsync();
// All 'Employee' entities are loaded and will be tracked by the session
or
// load all entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'Employees/ByName' index
List<Employee> query = (from employee in session.Query<Employee, Employees_ByName>()
where employee.FirstName == "Robert"
select employee).ToList();
Custom methods and extensions
Available custom methods and extensions:
- AddTransformerParameter
- AggregateBy
- AnyAsync
- As
- AsProjection
- CountAsync
- CountLazily
- Customize
- FirstAsync
- FirstOrDefaultAsync
- Include
- Intersect
- Lazily
- LazilyAsync
OrderByScore()
- ProjectFromIndexFieldsInto
- Search
- SingleAsync
- SingleOrDefaultAsync
- Spatial
- Statistics
- Suggest
- SuggestAsync
- SuggestLazy
- ToFacetQuery
- ToFacets
- ToFacetsAsync
- ToFacetsLazy
- ToListAsync
- TransformWith