IDocumentClient.CreateDocumentQuery Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
CreateDocumentQuery(String, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service. It returns IOrderedQueryable{Document}. |
CreateDocumentQuery(Uri, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery(String, SqlQuerySpec, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{dynamic}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec. |
CreateDocumentQuery(String, String, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{dynamic}. |
CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery(Uri, String, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery<T>(Uri, String, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery<T>(String, String, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{T}. |
CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{T}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec. |
CreateDocumentQuery<T>(Uri, FeedOptions) |
Method to create a query for documents in the Azure Cosmos DB service. |
CreateDocumentQuery<T>(String, FeedOptions) |
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service. |
CreateDocumentQuery(String, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service. It returns IOrderedQueryable{Document}.
public System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document> CreateDocumentQuery (string collectionLink, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document>
Public Function CreateDocumentQuery (collectionLink As String, Optional feedOptions As FeedOptions = Nothing) As IOrderedQueryable(Of Document)
Parameters
- collectionLink
- String
The link to the parent document collection.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
An IOrderedQueryable{Document} that can evaluate the query.
Examples
This example below queries for documents by id.
Document document = client.CreateDocumentQuery<Document>(collectionLink)
.Where(d => d.Id == "War and Peace").AsEnumerable().FirstOrDefault();
Remarks
This overload should be used when the schema of the queried documents is unknown or when querying by ID and replacing/deleting documents. Since Document is a DynamicObject, it can be dynamically cast back to the original C# object.
See also
Applies to
CreateDocumentQuery(Uri, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document> CreateDocumentQuery (Uri documentCollectionUri, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<Microsoft.Azure.Documents.Document>
Public Function CreateDocumentQuery (documentCollectionUri As Uri, Optional feedOptions As FeedOptions = Nothing) As IOrderedQueryable(Of Document)
Parameters
- documentCollectionUri
- Uri
The URI of the document collection.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery(String, SqlQuerySpec, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{dynamic}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.
public System.Linq.IQueryable<dynamic> CreateDocumentQuery (string collectionLink, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>
Public Function CreateDocumentQuery (collectionLink As String, querySpec As SqlQuerySpec, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of Object)
Parameters
- collectionLink
- String
The link to the parent document collection.
- querySpec
- SqlQuerySpec
The SqlQuerySpec instance containing the SQL expression.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
an IQueryable{dynamic> that can evaluate the query.
Examples
This example below queries for book documents.
// SQL querying allows dynamic property access
var query = new SqlQuerySpec(
"SELECT * FROM books b WHERE b.title = @title",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
dynamic document = client.CreateDocumentQuery<dynamic>(collectionLink, query).AsEnumerable().FirstOrDefault();
Remarks
Refer to https://docs.microsoft.com/azure/cosmos-db/sql-query-getting-started/ for syntax and examples.
See also
Applies to
CreateDocumentQuery(String, String, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{dynamic}.
public System.Linq.IQueryable<dynamic> CreateDocumentQuery (string collectionLink, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>
Public Function CreateDocumentQuery (collectionLink As String, sqlExpression As String, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of Object)
Parameters
- collectionLink
- String
The link to the parent document collection.
- sqlExpression
- String
The SQL statement.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
an IQueryable{dynamic> that can evaluate the query.
Examples
This example below queries for book documents.
// SQL querying allows dynamic property access
dynamic document = client.CreateDocumentQuery<dynamic>(collectionLink,
"SELECT * FROM books b WHERE b.title == 'War and Peace'").AsEnumerable().FirstOrDefault();
Remarks
Refer to https://docs.microsoft.com/azure/cosmos-db/sql-query-getting-started/ for syntax and examples.
See also
Applies to
CreateDocumentQuery(Uri, SqlQuerySpec, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IQueryable<dynamic> CreateDocumentQuery (Uri documentCollectionUri, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>
Public Function CreateDocumentQuery (documentCollectionUri As Uri, querySpec As SqlQuerySpec, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of Object)
Parameters
- documentCollectionUri
- Uri
The URI of the document collection.
- querySpec
- SqlQuerySpec
The sql query.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery(Uri, String, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IQueryable<dynamic> CreateDocumentQuery (Uri documentCollectionUri, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<obj>
Public Function CreateDocumentQuery (documentCollectionUri As Uri, sqlExpression As String, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of Object)
Parameters
- documentCollectionUri
- Uri
The URI of the document collection.
- sqlExpression
- String
The sql query.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery<T>(Uri, SqlQuerySpec, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>
Public Function CreateDocumentQuery(Of T) (documentCollectionUri As Uri, querySpec As SqlQuerySpec, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- documentCollectionUri
- Uri
The URI of the document collection.
- querySpec
- SqlQuerySpec
The sql query.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery<T>(Uri, String, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>
Public Function CreateDocumentQuery(Of T) (documentCollectionUri As Uri, sqlExpression As String, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- documentCollectionUri
- Uri
The URI of the document collection.
- sqlExpression
- String
The sql query.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery<T>(String, String, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement. It returns an IQueryable{T}.
public System.Linq.IQueryable<T> CreateDocumentQuery<T> (string collectionLink, string sqlExpression, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>
Public Function CreateDocumentQuery(Of T) (collectionLink As String, sqlExpression As String, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- collectionLink
- String
The link to the parent collection.
- sqlExpression
- String
The SQL statement.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
An IQueryable{T} that can evaluate the query.
Examples
This example below queries for some book documents.
public class Book
{
[JsonProperty("title")]
public string Title {get; set;}
public Author Author {get; set;}
public int Price {get; set;}
}
public class Author
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
// Query by the Title property
Book book = client.CreateDocumentQuery<Book>(collectionLink,
"SELECT * FROM books b WHERE b.title = 'War and Peace'").AsEnumerable().FirstOrDefault();
// Query a nested property
Book otherBook = client.CreateDocumentQuery<Book>(collectionLink,
"SELECT * FROM books b WHERE b.Author.FirstName = 'Leo'").AsEnumerable().FirstOrDefault();
// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
foreach (Book matchingBook in client.CreateDocumentQuery<Book>(
collectionLink, "SELECT * FROM books b where b.Price > 1000"))
{
// Iterate through books
}
// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
using (var queryable = client.CreateDocumentQuery<Book>(collectionLink,
"SELECT * FROM books b WHERE b.title = 'War and Peace'",
new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery())
{
while (queryable.HasMoreResults)
{
foreach(Book b in await queryable.ExecuteNextAsync<Book>())
{
// Iterate through books
}
}
}
Remarks
Refer to https://docs.microsoft.com/azure/cosmos-db/sql-query-getting-started/ for syntax and examples.
See also
Applies to
CreateDocumentQuery<T>(String, SqlQuerySpec, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB database using a SQL statement with parameterized values. It returns an IQueryable{T}. For more information on preparing SQL statements with parameterized values, please see SqlQuerySpec.
public System.Linq.IQueryable<T> CreateDocumentQuery<T> (string collectionLink, Microsoft.Azure.Documents.SqlQuerySpec querySpec, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.SqlQuerySpec * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IQueryable<'T>
Public Function CreateDocumentQuery(Of T) (collectionLink As String, querySpec As SqlQuerySpec, Optional feedOptions As FeedOptions = Nothing) As IQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- collectionLink
- String
The link to the parent document collection.
- querySpec
- SqlQuerySpec
The SqlQuerySpec instance containing the SQL expression.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
An IQueryable{T} that can evaluate the query.
Examples
This example below queries for some book documents.
public class Book
{
[JsonProperty("title")]
public string Title {get; set;}
public Author Author {get; set;}
public int Price {get; set;}
}
public class Author
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
// Query using Title
Book book, otherBook;
var query = new SqlQuerySpec(
"SELECT * FROM books b WHERE b.title = @title",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
book = client.CreateDocumentQuery<Book>(collectionLink, query).AsEnumerable().FirstOrDefault();
// Query a nested property
query = new SqlQuerySpec(
"SELECT * FROM books b WHERE b.Author.FirstName = @firstName",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@firstName", Value = "Leo" }}));
otherBook = client.CreateDocumentQuery<Book>(collectionLink, query).AsEnumerable().FirstOrDefault();
// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
query = new SqlQuerySpec(
"SELECT * FROM books b WHERE b.Price > @minPrice",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@minPrice", Value = 1000 }}));
foreach (Book b in client.CreateDocumentQuery<Book>(
collectionLink, query))
{
// Iterate through books
}
// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
query = new SqlQuerySpec(
"SELECT * FROM books b WHERE b.title = @title",
new SqlParameterCollection(new SqlParameter[] { new SqlParameter { Name = "@title", Value = "War and Peace" }}));
using (var queryable = client.CreateDocumentQuery<Book>(collectionLink, query,
new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery())
{
while (queryable.HasMoreResults)
{
foreach(Book b in await queryable.ExecuteNextAsync<Book>())
{
// Iterate through books
}
}
}
Remarks
Refer to https://docs.microsoft.com/azure/cosmos-db/sql-query-getting-started/ for syntax and examples.
See also
Applies to
CreateDocumentQuery<T>(Uri, FeedOptions)
Method to create a query for documents in the Azure Cosmos DB service.
public System.Linq.IOrderedQueryable<T> CreateDocumentQuery<T> (Uri documentCollectionUri, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : Uri * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<'T>
Public Function CreateDocumentQuery(Of T) (documentCollectionUri As Uri, Optional feedOptions As FeedOptions = Nothing) As IOrderedQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- documentCollectionUri
- Uri
The URI of the parent document collection.
- feedOptions
- FeedOptions
(Optional) The FeedOptions for processing the query results feed.
Returns
The query result set.
Applies to
CreateDocumentQuery<T>(String, FeedOptions)
Overloaded. This method creates a query for documents under a collection in an Azure Cosmos DB service.
public System.Linq.IOrderedQueryable<T> CreateDocumentQuery<T> (string collectionLink, Microsoft.Azure.Documents.Client.FeedOptions feedOptions = default);
abstract member CreateDocumentQuery : string * Microsoft.Azure.Documents.Client.FeedOptions -> System.Linq.IOrderedQueryable<'T>
Public Function CreateDocumentQuery(Of T) (collectionLink As String, Optional feedOptions As FeedOptions = Nothing) As IOrderedQueryable(Of T)
Type Parameters
- T
The type of object to query.
Parameters
- collectionLink
- String
The link to the parent collection resource.
- feedOptions
- FeedOptions
The options for processing the query result feed. For details, see FeedOptions
Returns
An IOrderedQueryable{T} that can evaluate the query.
Examples
This example below queries for some book documents.
public class Book
{
[JsonProperty("title")]
public string Title {get; set;}
public Author Author {get; set;}
public int Price {get; set;}
}
public class Author
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
// Query by the Title property
Book book = client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Title == "War and Peace").AsEnumerable().FirstOrDefault();
// Query a nested property
Book otherBook = client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Author.FirstName == "Leo").AsEnumerable().FirstOrDefault();
// Perform a range query (needs an IndexType.Range on price or FeedOptions.EnableScansInQuery)
foreach (Book matchingBook in client.CreateDocumentQuery<Book>(collectionLink).Where(b => b.Price > 100))
{
// Iterate through books
}
// Query asychronously. Optionally set FeedOptions.MaxItemCount to control page size
using (var queryable = client.CreateDocumentQuery<Book>(
collectionLink,
new FeedOptions { MaxItemCount = 10 })
.Where(b => b.Title == "War and Peace")
.AsDocumentQuery())
{
while (queryable.HasMoreResults)
{
foreach(Book b in await queryable.ExecuteNextAsync<Book>())
{
// Iterate through books
}
}
}
Remarks
The Azure Cosmos DB LINQ provider compiles LINQ to SQL statements. Refer to https://docs.microsoft.com/azure/cosmos-db/sql-query-linq-to-sql for the list of expressions supported by the Azure Cosmos DB LINQ provider. ToString() on the generated IQueryable returns the translated SQL statement. The Azure Cosmos DB provider translates JSON.NET and DataContract serialization attributes for members to their JSON property names.