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>(); // perform query on a dynamic index
IRavenQueryable<T> Query<T>(string indexName, 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. |
Safe By Default
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
// load up to 128 entities from 'Employees' collection
List<Employee> employees = session
.Query<Employee>()
.ToList();
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.
// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
List<Employee> employees = session
.Query<Employee>()
.Where(x => x.FirstName == "Robert")
.ToList();
// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
IRavenQueryable<Employee> employees = from employee in session.Query<Employee>()
where employee.FirstName == "Robert"
select employee;
Example III - Querying specified index
// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
IRavenQueryable<Employee> employees = from employee in session.Query<Employee>("My/Custom/Index")
where employee.FirstName == "Robert"
select employee;
or
// load up to 128 entities from 'Employees' collection
// where FirstName equals 'Robert'
// using 'My/Custom/Index'
IRavenQueryable<Employee> employees = from employee in session.Query<Employee, MyCustomIndex>()
where employee.FirstName == "Robert"
select employee;
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